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

Why is this script not working as it is supposed to be doing? (It is a sword/baton tool)

local AbleToDamage = true

local Damage = 30

  

script.Parent.Handle.Touched:Connect(function(Hit)

script.Parent.Activated:Connect(function()

if AbleToDamage == false then return end

AbleToDamage = false

if Hit.Parent:FindFirstChild("Humanoid") then

Hit.Parent.Humanoid:TakeDamage(Damage)

end

wait(0.5)

AbleToDamage = true

end)

end)

I know that the problem is that the hit value saves. How do I fix it so it looks for a new value each time I activate the tool?

1 comment

0

Please provide a bit more background detail.

1 answer

local AbleToDamage = true
local Damage = 30

local function Activated ()
	script.Parent.Handle.Touched:Connect(function(Hit)
		if AbleToDamage == true then
			AbleToDamage = false
			local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
			if Humanoid then
				Humanoid:TakeDamage(Damage)
			end
			wait(0.5)
			AbleToDamage = true
		end
	end
end

script.Parent.Activated:Connect(Activated)

I noticed a problem, if you activate the tool it would listen for the touched event but after that it would still continue to listen for the touched event, so if your still holding the tool if someone touches it, it would damage them. I didn't really know what was wrong with it, so i tried to re-create it and while doing so i also tried to "improve" it.

You can try this, i added something so that it doesn't still listen for the touched event after its activated.

local AbleToDamage = true
local Damage = 30
local Connection

local function Activated ()
	Connection = script.Parent.Handle.Touched:Connect(function(Hit)
    	if AbleToDamage == true then
    		AbleToDamage = false
    		local Humanoid = Hit.Parent:FindFirstChildWhichIsA("Humanoid")
    		if Humanoid then
    			Humanoid:TakeDamage(Damage)
	    	end
    		Connection:Disconnect()
    		wait(0.5)
    		AbleToDamage = true
		else
    		Connection:Disconnect()
		end
	end)
end

script.Parent.Activated:Connect(Activated)

2 comments

0

I don't think the hit value is the problem.. well anyway i think this is right.. i think, oh and you should use animations.

0

if your gonna use them, then put the code to play it right after "script.Parent.Handle.Touched:Connect(function(Hit)"

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