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

My TotalAmount resets for some reason, why?

Asked by
rabbi99 714 Moderation Voter
3 years ago

So I have a script where you can break a tree and get wood from it. From 1 tree you get 20 wood. If I break 1 tree I get 20 wood without any problems. However, if I break another tree, the totalamount becomes 0 and then becomes 20 again. I can't figure it out.

This is my localscript:

local rp = game:GetService("ReplicatedStorage")
local TreeHarvest = rp:WaitForChild("TreeHarvest")
local player = game.Players.LocalPlayer
totalamount = 0

TreeHarvest.OnClientEvent:connect(function(plr, amount)
    if player == plr then

        local totalamount = totalamount + amount
        print("You now have", totalamount, "wood.")
        script.Parent.Text = totalamount
    end 
end)

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Try doing this:

local rp = game:GetService("ReplicatedStorage")
local TreeHarvest = rp:WaitForChild("TreeHarvest")
local player = game.Players.LocalPlayer
totalamount = 0

TreeHarvest.OnClientEvent:connect(function(plr, amount)
    if player == plr then

        local totalamount = tonumber(script.Parent.Text)
        if totalamount == "" then
            local totalamount = 0
        end
        local totalamount = totalamount + amount
        print("You now have", totalamount, "wood.")
        script.Parent.Text = totalamount
    end 
end)
Ad

Answer this question