Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I need help on a Shop Gui Button Can you help please?

Asked by 9 years ago

So recently, I have made this script that should connect to my leaderboard that does work but it never buys the item... Could you please see what's wrong and fix it for me?

-- Leaderboard Script --
        if type == "Individual" then
            if winner and credits then
                if winner.Character and winner:FindFirstChild("leaderstats") then
                    if winner.leaderstats:FindFirstChild("Credits") then
                        winner.leaderstats.Credits.Value = winner.leaderstats.Credits.Value + credits
                    end
                end
            end
-- Shop button script --

player = script.Parent.Parent.Parent.Parent.Parent
Credits = player.leaderstats.KOs -- My leaderboard doesn't use KOs do I change it with credits?
price = 150
tool = game.ServerStorage:findFirstChild("ItemName")


function buy()
if Credits.Value >= price then
Credits.Value = Credits.Value - price
local a = tool:clone()
a.Parent = player.Backpack
local b = tool:clone()
b.Parent = player.StarterGear

end
end
script.Parent.MouseButton1Down:connect(buy)

1 answer

Log in to vote
0
Answered by 9 years ago

You're correct, you need to change KOs to Credits since you're not using KOs in your leaderboard. Also, ItemName should be the name of the tool you want to buy.

You may want to either use WaitForChild as opposed to FindFirstChild as FindFirstChild could return nil and cause your script to error when trying to clone the tool if it's not there. Also, you may want to use this in a LocalScript and move the tool to ReplicatedStorage to make things easier and to avoid errors with parenting (like in your old script)

Another thing you could do (but isn't necessary) is to make the buy function anonymous. What I mean is wrapping the function in your connection line so you don't have to call it when connecting an event to it.

If you followed my recommendations, you should get something like this:

-- Shop button script --

player = game.Players.LocalPlayer --With a LocalScript, you can access the LocalPlayer property of the Players service, which gets the player on the client.
Credits = player.leaderstats.Credits
price = 150 --Change this to the price you want.
tool = game.ReplicatedStorage:WaitForChild("ItemName") --Change ItemName to the name of your tool. Make sure your tool is in ReplicatedStorage.

script.Parent.MouseButton1Down:connect(function() --Anonymous function. See how the function is wrapped in your connect function.
    if Credits.Value >= price then
        Credits.Value = Credits.Value - price
        local a = tool:clone()
        a.Parent = player.Backpack
        local b = tool:clone()
        b.Parent = player.StarterGear
    end
end) --Closing parenthesis at the end of the function to completely wrap the function in your connection line.

I hope this helped you. Please accept my answer (it's the tick button to the right of this answer) if it helped.

Learn more about anonymous functions here.

Ad

Answer this question