Search code examples
luaroblox

Lua Scripts disabling each other in Roblox Studio


I'm trying to make a game in Roblox (a tycoon) and, considering it's my first time making a game, I'm using some pre-made scripts and models along with youtube tutorials. My problem right now is that I have two scripts, both under the same name, DevProductHandler, (I've tried changing their names but the problem still persists) and it seems like only one will run at a time whenever I test the game. Disabling one script allows the other to run perfectly fine, and enabling both causes only one script to work, randomly picking which one will run each time.

One of the scripts is meant to control purchasing products that are prompted from a GUI, and the other controls purchases prompted from tycoon buttons that you walk on top of. I've tried merging the scripts by copy-pasting one underneath the other, but it still only makes the GUI DevProductHandler script work. I don't know if there are some overlapping variables or something that would cause one script to not allow the other to run, but I think that's most likely the issue, although I'm not sure how to go about fixing that I'm new at Lua and game creation so I was wondering if anyone could help me find out the issue. These are the scripts.

local Tycoon = script.Parent.Tycoons:GetChildren()[1]
script.Parent = game.ServerScriptService
local DevProducts = {}
local MarketplaceService = game:GetService('MarketplaceService')

for i,v in pairs(Tycoon:WaitForChild('Buttons'):GetChildren()) do
    if v:FindFirstChild('DevProduct') then
        if v.DevProduct.Value > 0 then
            DevProducts[v.DevProduct.Value] = v -- the button
        end
    end
end

MarketplaceService.ProcessReceipt = function(receiptInfo)
    for i,plr in pairs(game.Players:GetPlayers()) do
        if plr.userId == receiptInfo.PlayerId then
            if DevProducts[receiptInfo.ProductId] then
                local obj = DevProducts[receiptInfo.ProductId]
                local PlrT = game.ServerStorage.PlayerMoney:WaitForChild(plr.Name).OwnsTycoon
                if PlrT.Value ~= nil then
                    --if PlrT.Value.PurchasedObjects:FindFirstChild(obj.Object.Value) == false then
                        local PlayerStats = game.ServerStorage.PlayerMoney:FindFirstChild(plr.Name)
                        Create({[1] = 0,[2] = obj,[3] = PlayerStats}, PlrT.Value.BuyObject)
                    --end
                end
            end
        end
    end
end

function Create(tab, prnt)
    local x = Instance.new('Model')
    Instance.new('NumberValue',x).Value = tab[1]
    x.Value.Name = "Cost"
    Instance.new('ObjectValue',x).Value = tab[2]
    x.Value.Name = "Button"
    local Obj = Instance.new('ObjectValue',x)
    Obj.Name = "Stats"
    Obj.Value = tab[3]
    x.Parent = prnt
end

Above is the script for prompts from walking over buttons.

old_fog = game.Lighting.FogStart
local MarketplaceService = game:GetService("MarketplaceService")

function getPlayerFromId(id)
    for i,v in pairs(game.Players:GetChildren()) do
        if v.userId == id then
            return v
        end
    end
    return nil
end

MarketplaceService.ProcessReceipt = function(receiptInfo)
    local productId = receiptInfo.ProductId
    local playerId = receiptInfo.PlayerId
    local player = getPlayerFromId(playerId)
    local productName 


    if productId == 1172271849 then
        local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
        if cashmoney then
            cashmoney.Value = cashmoney.Value + 10000
        end

    elseif productId == 1172270951 then
        local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
        if cashmoney then
            cashmoney.Value = cashmoney.Value + 100000
        end     

    elseif productId == 1172270763 then
        local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
        if cashmoney then
            cashmoney.Value = cashmoney.Value + 1000000
        end 

    elseif productId == 1172272327 then
        local cashmoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
        if cashmoney then
            cashmoney.Value = cashmoney.Value + cashmoney.Value
        end 

    elseif productId == 1172273117 then
        local char = player.Character
        if char then
            local human = char:FindFirstChild("Humanoid")
            if human then
                human.WalkSpeed = human.WalkSpeed + human.WalkSpeed
            end
        end


    elseif productId == 1172273437 then
        game.ServerStorage.HyperlaserGun:Clone().Parent=player.Backpack

    elseif productId == 1172272691 then
        game.ServerStorage.FlyingCarpet:Clone().Parent=player.Backpack 
    end
    return Enum.ProductPurchaseDecision.PurchaseGranted     
end

Above is the script for purchases from a GUI prompt.


Solution

  • From the Roblox manual:

    As with all callbacks, this function should be set once and only once by a single Script. If you're selling multiple products in your game, this callback must handle receipts for all of them.

    You're implementing game:GetService('MarketplaceService').ProcessReceipt in both scripts. One overwrite the other.