Ad
Log in to vote
-2

Why won't the following shop GUI script function? [Solved]

Asked by 7 years ago
player = script.Parent.Parent.Parent.Parent.Parent.Parent
cost = 0


function checkForBat()
    found = false
    for i, v in pairs(player.Backpack.GetChildren()) do
        if v.Name == "Bat" then
            found = true
        end
    end
    for i, v in pairs(player.Character.GetChildren()) do
        if v.Name == "Bat" then
            found = true
        end
    end
    if found == true then
        return true
    else
        return false
    end
end


script.MouseButton1Click:connect(function()
    leaderstats = player.FindFirstChild("leaderstats")
       if leaderstats then
           Money = leaderstats.FindFirstchild("Money")
        if Money then
           hasBat = checkForBat()
              if Money.Value >= cost and hasBat == false then
               Money.Value = Money.Value - cost
               Bat = game.Lighting.Bat:Clone()
               Bat.Parent = player.Backpack
        end
    end
    end
    end)

Nothing happens. The GUI is working perfectly, the script just won't do anything....

** The place link is >http://www.roblox.com/The-Purge-place?id=158668406< if it'll help.**

0
You can use `game.Players.LocalPlayer` instead of the `script.Parent.Parent`... mess. gskw 1046 — 7y
0
Thanks. nightmare13542 45 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago
player = game.Players.LocalPlayer --This only works in a local script.
cost = 0 --assign the cost of the bat here.

function checkForBat()
    local found = false
    for i, v in pairs(player.Backpack:GetChildren()) do
        if v.Name == "Bat" then
            found = true
        end
    end
    for i, v in pairs(player.Character:GetChildren()) do
        if v.Name == "Bat" then
            found = true
        end
    end
    return found
end


GuiButtonPath.MouseButton1Click:connect(function()
    local needsBat = not checkForBat() --needsBat is the opposite of found.
    leaderstats = player:FindFirstChild("leaderstats")
    if leaderstats ~= nil then
        Money = leaderstats:FindFirstchild("Money")
        if Money ~= nil then
            if Money.Value >= cost and needsBat then --if they have the money and they need a bat
                Money.Value = Money.Value - cost --subtract cost from funds
                Bat = game.Lighting.Bat:Clone()
                Bat.Parent = player.Backpack --give them a bat
            end
        end
    end
end)
0
Thanks! You're great! One more question, if I was the use a script to hide the backpack, then enable the backpack again, would the tools still be there? nightmare13542 45 — 7y
0
I don't know for sure, but my guess would be yes. GoldenPhysics 472 — 7y
Ad
Log in to vote
0
Answered by
ultrabug 200 Moderation Voter
7 years ago

Okay, so I noticed that you are not actually using the Methods on the values you call, for instance: Money = leaderstats.FindFirstchild("Money") But FindFirstChild is a method, you have to use colons not a period, that goes for the GetChildren method too.

0
Im a bit new to scripting... Not sure what you mean. :/ nightmare13542 45 — 7y

Answer this question