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 |
03 | player = script.Parent.Parent.Parent.Parent.Parent |
04 | Credits = player.leaderstats.KOs -- My leaderboard doesn't use KOs do I change it with credits? |
05 | price = 150 |
06 | tool = game.ServerStorage:findFirstChild( "ItemName" ) |
07 |
08 |
09 | function buy() |
10 | if Credits.Value > = price then |
11 | Credits.Value = Credits.Value - price |
12 | local a = tool:clone() |
13 | a.Parent = player.Backpack |
14 | local b = tool:clone() |
15 | b.Parent = player.StarterGear |
16 |
17 | end |
18 | end |
19 | script.Parent.MouseButton 1 Down:connect(buy) |
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 |
03 | player = game.Players.LocalPlayer --With a LocalScript, you can access the LocalPlayer property of the Players service, which gets the player on the client. |
04 | Credits = player.leaderstats.Credits |
05 | price = 150 --Change this to the price you want. |
06 | tool = game.ReplicatedStorage:WaitForChild( "ItemName" ) --Change ItemName to the name of your tool. Make sure your tool is in ReplicatedStorage. |
07 |
08 | script.Parent.MouseButton 1 Down: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 |
16 | 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.