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

Why can my Script can find a value once in a function and then never again?

I'm using 2 scripts to change the value. A server script inside a tool in the Player's backpack and a local script inside the same tool in the Player's backpack.

Damage is an IntValue replacing a BoolValue since the BoolValue didn't change. So I was using an IntValue as a replacement. The problem is that on the server script, it can't find that Damage.Value = 1 When it is. I can see the value change once on properties when I play to test it.

Script:

local Damage = script.Parent.Damage
script.Parent.Blade.Touched:connect(function(p)--If the blade(Tool Hitbox) touches something
	print("got",p)--print debug, shows what the blade has touched
	if p.Parent:FindFirstChild("Humanoid") then--if the touched item is from a Humanoid

		if Damage.Value == 1 then --if Damage.Value is true. It only finds this change once (Was a BoolValue but the BoolValue couldn't find the value change so I made the value an IntValue instead. Didn't help.)

			print("damage is true")--print debug, only appears once 

			Damage.Value = 2 --changes the value of damage. Only changes once. 

			print("damage =",Damage.Value)--print debug

			p.Parent.Humanoid.Health = p.Parent.Humanoid.Health - 20--lowers the humanoid health by 20
		end
	end
end)

Local Script:

local CanAttack = true
local Swing = script.Parent:WaitForChild("Blade"):WaitForChild("Swing")
local Trail = script.Parent:WaitForChild("SwordStem"):WaitForChild("Trail")
script.Parent.Activated:connect(function()
	local attack = script.Parent.Parent.Humanoid:LoadAnimation(script.Attack)--loads animation
	if CanAttack == true then--if player can use tool
		attack:Play()--plays animation
		Swing:Play()--plays sound
		Trail.Transparency = NumberSequence.new(0.1,0.5)--never mind this
		CanAttack = false--stops player from attacking
		wait(0.5)--cooldown
		attack:Stop()--stops animation
		Trail.Transparency = NumberSequence.new(0.1,1)--never mind this
		CanAttack = true--if player can use tool again
		script.Parent.Damage.Value = 1--Here is where the value changes
	end
end)

1 answer

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