Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My TotalAmount resets for some reason, why?

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

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.