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 =^)
player = game.Players.LocalPlayer:findFirstChild("leaderstats") money2 = player:findFirstChild("Moneys") money = player:findFirstChild("Candy") price = 6 extra = 2 text = { "Here you go! Would you like to trade more candy?"} function Click() wait() if money.Value >= price then money.Value = money.Value - price money2.Value = money2.Value + extra local randText = text[math.random(#text)] for a=1,string.len(randText) do script.Parent.Parent.Dialog.Text = string.sub(randText,1,a).."" wait(.01) end end end script.Parent.MouseButton1Down:connect(Click)
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.
local player = game.Players.LocalPlayer:FindFirstChild("leaderstats") local money2 = player:findFirstChild("Moneys") local money = player:findFirstChild("Candy") local price = 6 local extra = 2 local text = { "Here you go! Would you like to trade more candy?"} function Click() wait() if money.Value >= price then money.Value = money.Value - price money2.Value = money2.Value + extra local randText = text[math.random(#text)] for a=1,string.len(randText) do script.Parent.Parent.Dialog.Text = string.sub(randText,1,a).."" wait(.01) end end end script.Parent.MouseButton1Down:connect(Click)
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:
local remote = game.ReplicatedStorage.TradeCandy --This is your remote local q = "Would you like to trade more candy?" local price,extra,text = 6,2,{ "Here you go!"; "Omnom.. thank you for your contributions."; "Heck yeah keep the candy train rolling!"; } function Click() if money.Value >= price then local randText = text[math.random(#text)]..q remote:FireServer(price,extra,randText); --FireServer end end script.Parent.MouseButton1Down:Connect(Click) --Use Connect!
Script in ServerScriptStorage:
local remote = game.ReplicatedStorage.TradeCandy --This is your remote remote.OnServerEvent:Connect(function(plr,cost,award,msg) local money2 = plr:WaitForChild("Moneys") --Use WaitForChild local money = plr:WaitForChild("Candy") money.Value = money.Value - cost money2.Value = money2.Value + award for a = 1, #msg do --# operator! script.Parent.Parent.Dialog.Text = msg:sub(1,a) wait() end end)