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 10 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?

1-- Leaderboard Script --
2        if type == "Individual" then
3            if winner and credits then
4                if winner.Character and winner:FindFirstChild("leaderstats") then
5                    if winner.leaderstats:FindFirstChild("Credits") then
6                        winner.leaderstats.Credits.Value = winner.leaderstats.Credits.Value + credits
7                    end
8                end
9            end
01-- Shop button script --
02 
03player = script.Parent.Parent.Parent.Parent.Parent
04Credits = player.leaderstats.KOs -- My leaderboard doesn't use KOs do I change it with credits?
05price = 150
06tool = game.ServerStorage:findFirstChild("ItemName")
07 
08 
09function buy()
10if Credits.Value >= price then
11Credits.Value = Credits.Value - price
12local a = tool:clone()
13a.Parent = player.Backpack
14local b = tool:clone()
15b.Parent = player.StarterGear
16 
17end
18end
19script.Parent.MouseButton1Down:connect(buy)

1 answer

Log in to vote
0
Answered by 10 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:

01-- Shop button script --
02 
03player = game.Players.LocalPlayer --With a LocalScript, you can access the LocalPlayer property of the Players service, which gets the player on the client.
04Credits = player.leaderstats.Credits
05price = 150 --Change this to the price you want.
06tool = game.ReplicatedStorage:WaitForChild("ItemName") --Change ItemName to the name of your tool. Make sure your tool is in ReplicatedStorage.
07 
08script.Parent.MouseButton1Down:connect(function() --Anonymous function. See how the function is wrapped in your connect function.
09    if Credits.Value >= price then
10        Credits.Value = Credits.Value - price
11        local a = tool:clone()
12        a.Parent = player.Backpack
13        local b = tool:clone()
14        b.Parent = player.StarterGear
15    end
16end) --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