So I have made a script that is suppose to take away Candy from the player and give them Cash instead, it is located in a button inside a gui, however, it doesn't seem to work properly. It takes away the candy and adds money, but it does not save.
I initially thought it was an issue with the datastore but it turns out that the issue is within the script, thanks =^)
01 | player = game.Players.LocalPlayer:findFirstChild( "leaderstats" ) |
02 | money 2 = player:findFirstChild( "Moneys" ) |
03 | money = player:findFirstChild( "Candy" ) |
04 | price = 6 |
05 | extra = 2 |
06 |
07 | text = { "Here you go! Would you like to trade more candy?" } |
08 |
09 |
10 | function Click() |
11 | wait() |
12 | if money.Value > = price then |
13 | money.Value = money.Value - price |
14 | money 2. Value = money 2. Value + extra |
15 | local randText = text [ math.random(#text) ] |
Judging by the variables, I'm assuming this is inside of a server script. You can only use LocalPlayer within a LocalScript, because it is trying to define the player. It is also trying access the leaderstats property within the player.
So, try putting the code in a LocalScript and settings the variables to local as well, like this.
01 | local player = game.Players.LocalPlayer:FindFirstChild( "leaderstats" ) |
02 | local money 2 = player:findFirstChild( "Moneys" ) |
03 | local money = player:findFirstChild( "Candy" ) |
04 | local price = 6 |
05 | local extra = 2 |
06 |
07 | local text = { "Here you go! Would you like to trade more candy?" } |
08 |
09 | function Click() |
10 | wait() |
11 | if money.Value > = price then |
12 | money.Value = money.Value - price |
13 | money 2. Value = money 2. Value + extra |
14 | local randText = text [ math.random(#text) ] |
15 | for a = 1 ,string.len(randText) do |
Also check for any output errors as well.
First and foremost, make this a LocalScript inside the GuiButton if it isn't.
And secondly, your problem is probably that the player does not own a leaderstats, Moneys, or Candy object yet. Use WaitForChild
.
And for the efficiency fixes..
tab your code correctly.
connect
is deprecated - use Connect
.
You can use the #
operator to get the length of a string.
For this to replicate, you have to use remotes :)
Use the FireServer
function from the client, and execute the code on the server in an OnServerEvent
event.
LocalScript:
01 | local remote = game.ReplicatedStorage.TradeCandy --This is your remote |
02 | local q = "Would you like to trade more candy?" |
03 | local price,extra,text = 6 , 2 , { |
04 | "Here you go!" ; |
05 | "Omnom.. thank you for your contributions." ; |
06 | "Heck yeah keep the candy train rolling!" ; |
07 | } |
08 |
09 |
10 | function Click() |
11 | if money.Value > = price then |
12 | local randText = text [ math.random(#text) ] ..q |
13 | remote:FireServer(price,extra,randText); --FireServer |
14 | end |
15 | end |
16 |
17 | script.Parent.MouseButton 1 Down:Connect(Click) --Use Connect! |
Script in ServerScriptStorage:
01 | local remote = game.ReplicatedStorage.TradeCandy --This is your remote |
02 |
03 | remote.OnServerEvent:Connect( function (plr,cost,award,msg) |
04 | local money 2 = plr:WaitForChild( "Moneys" ) --Use WaitForChild |
05 | local money = plr:WaitForChild( "Candy" ) |
06 | money.Value = money.Value - cost |
07 | money 2. Value = money 2. Value + award |
08 | for a = 1 , #msg do --# operator! |
09 | script.Parent.Parent.Dialog.Text = msg:sub( 1 ,a) |
10 | wait() |
11 | end |
12 | end ) |