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

Value doesn't seem to be updating correctly inside of my script?

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

0

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.

0

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.

0

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

1 answer

I figured out a solution to this problem and I'll write it here for future reference:

I instead put a LocalScript inside of the gun in which the script is titled SmallAmmoRestore and inside of that script is the function that gives the gun more StoredAmmo. The part that I wanted to give players ammo now instead enables that script so that the function may fire once, then the part disables the script so that it no longer fires. This method has proven successful to me; although, the only problem I've had with it is that if two ammo boxes are really close to each other they will sometimes register as only one or more than two pick ups. This isn't really a problem if the ammo is spaced out though.

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