What's wrong with my stick script

Hello
I’m trying to make a simple stick tool.
The script I made should be working, but it’s strange and buggy

It only sometimes works. I’ll share the script, my ears are open for feedback.

I’ve just tried using my own head, this should work.

local touching = false
local debounce = false
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if hit.Name == script.Parent.PlayerName.Name then
		else
			touching = true
		end		
	end
	
	script.Parent.TouchEnded:Connect(function()
		touching = false
	end)
	script.Parent.Parent.Activated:Connect(function()

		if touching == true then
			if debounce == false then
				
			debounce = true
		
			if hit.Parent:FindFirstChild("Humanoid") then

				hit.Parent:FindFirstChild("Humanoid").Health -= 5
			
					wait(1)
					debounce = false
				end
			end
		else
	end
end)	
end)

Touched events can be rather challenging to work with. I would suggest maybe you move your damage and debounce into the Touched event and just set a flag to true in the Activated event, wait a second, and set it back to false. You’d also check in the Touched event to make sure the flag is true before giving the damage.

It looks like the way you currently have it you’d need to touch the person with the stick and then click the button to cause damage.

1 Like

Here is the revised script: Same bugs

local flag = false
local debounce = false
script.Parent.Touched:Connect(function(hit)
	
	if hit.Parent:FindFirstChild("Humanoid") then
		if hit.Name == script.Parent.PlayerName.Name then
		else
			
			if flag == true then
				if hit.Parent:FindFirstChild("Humanoid") then

					hit.Parent:FindFirstChild("Humanoid").Health -= 5
				
				
				
				
			end
		end
		
		end
		
			
	end

	
	end)


script.Parent.Parent.Activated:Connect(function()

	flag = true
	wait(.1)
	flag = false


end)

This script worked for me. Are you putting this into a LocalScript in your tool?

What no lol.

I actually fixed it in a different way, thanks anyway

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.