I'm working on a survival game and I have a humanoid in the map that upon death clones a part from the Lighting that holds this script.
ammo = 1
function onTouched(hit)
local human = hit.Parent:findFirstChild("Humanoid")
local player = game.Players:findFirstChild(hit.Parent.Name)
if (human ~= nil) then
local Ammo = human.Parent.Carbine22.StoredAmmo
if (Ammo~=nil) then
Ammo.Value = Ammo.Value + ammo
script.Parent:remove()
end
end
end
script.Parent.Touched:connect(onTouched)
What the script is supposed to do is detect when a player is holding a tool called "Carbine22" and then update a value the tool has inside of it called "StoredAmmo".
I want the part to increase the ammo reserves of the player when they touch it. But, instead I'm encountering a strange interaction.
What should happen is: say the player has 25 StoredAmmo currently in their gun. When they touch the part with the script, their StoredAmmo should now be 26.
What instead happens is the StoredAmmo becomes what the initial StoredAmmo was before the player picked up the tool. So if the initial StoredAmmo was 30, the player's StoredAmmo goes from 25 to 31.
This would make sense if the StoredAmmo didn't update through the value and instead through a script, but when I look on the Workspace at my character and his tool's value of StoredAmmo, it is indeed updating. No matter what though, the StoredAmmo does not seem to be correctly increasing.
Any thoughts as to what I'm doing wrong?

3 comments
From what I'm getting it seems like you're setting the value of the ammo in a LocalScript when they pick it up or something. So once the touched event is actually fired it goes to 31 because on the server it actually has 30 instead of the 25 set locally.
The value of the ammo is being set in an IntValue that is located inside the tool. But, I've noticed the script seems to be storing the StoredAmmo value somewhere because even when the Part holding the script is destroyed and the player picks up another part with the script, the ammo keeps going up to for example: 31, then 32, then 33, no matter their StoredAmmo.
line 8 would be an irrelevant condition because you would get an error on line 7 before the script would even get to the condition