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

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)

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.

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.

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:

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