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

Script isn't properly subtracting and adding stats?

Asked by 7 years ago

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 =^)

01player = game.Players.LocalPlayer:findFirstChild("leaderstats")
02money2 = player:findFirstChild("Moneys")
03money = player:findFirstChild("Candy")
04price = 6
05extra = 2
06 
07text = {    "Here you go! Would you like to trade more candy?"}
08 
09 
10function Click()
11    wait()
12if money.Value >= price then
13money.Value = money.Value - price
14money2.Value = money2.Value + extra
15local randText = text[math.random(#text)]
View all 22 lines...

2 answers

Log in to vote
0
Answered by 7 years ago

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.

01local player = game.Players.LocalPlayer:FindFirstChild("leaderstats")
02local money2 = player:findFirstChild("Moneys")
03local money = player:findFirstChild("Candy")
04local price = 6
05local extra = 2
06 
07local text = {    "Here you go! Would you like to trade more candy?"}
08 
09function Click()
10    wait()
11if money.Value >= price then
12money.Value = money.Value - price
13money2.Value = money2.Value + extra
14local randText = text[math.random(#text)]
15for a=1,string.len(randText) do
View all 21 lines...

Also check for any output errors as well.

0
It doesn't change it unfortunately. A note to add on, when it takes away the candy, if the player gets more candy later on, it will combine it to the previous amount. For example, if I had 90 candy and I got rid of that and exchanged it for money but then soon got 6 candy, it would give me 96 instead of just 6. Skepticlemon 24 — 7y
Ad
Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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:

01local remote = game.ReplicatedStorage.TradeCandy --This is your remote
02local q = "Would you like to trade more candy?"
03local 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 
10function Click()
11    if money.Value >= price then
12        local randText = text[math.random(#text)]..q
13        remote:FireServer(price,extra,randText); --FireServer
14    end
15end
16 
17script.Parent.MouseButton1Down:Connect(Click) --Use Connect!

Script in ServerScriptStorage:

01local remote = game.ReplicatedStorage.TradeCandy --This is your remote
02 
03remote.OnServerEvent:Connect(function(plr,cost,award,msg)
04    local money2 = plr:WaitForChild("Moneys") --Use WaitForChild
05    local money = plr:WaitForChild("Candy")
06    money.Value = money.Value - cost
07    money2.Value = money2.Value + award
08    for a = 1, #msg do --# operator!
09        script.Parent.Parent.Dialog.Text = msg:sub(1,a)
10        wait()
11    end
12end)
0
It subtracts the candy and adds money, however, if the player collects more candy, it adds onto the previous amount. For example, if I traded 50 candy and I collect 6 candy later, instead of giving me 6 candy, it returns the whole 50 candy, giving me 56 candy. Skepticlemon 24 — 7y
0
Ahhh that's due to FE - i'll edit. Goulstem 8144 — 7y
0
Still has the same issue with the candy as I mentioned earlier. Skepticlemon 24 — 7y

Answer this question