My script is not working. Is this a bug or an error in the script?

Hello
I don’t think there’s a bug in the script, maybe my roblox studio is broken.
This is my script:

local part = script.Parent
local debounce = {}

local function onTouch(hit)
	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid and not debounce[character] then
		debounce[character] = true
		humanoid.WalkSpeed = humanoid.WalkSpeed - 10       -- -10 WalkSpeed
		part.Color = Color3.new(0, 1, 0)           -- change the color to Green
		
		task.wait(10)          
		
		humanoid.WalkSpeed = humanoid.WalkSpeed + 10   
		
		for i = 1, 10 do
			humanoid.Health = humanoid.Health - 10      -- -10 health every second
			task.wait(1)
		end
		
		task.wait(1)
		debounce[character] = false
		part.Color = Color3.new(1, 0, 0)  -- change the color back to red (The color of the part is red)
	end
end

part.Touched:Connect(onTouch)

When I touched the part, the part just turned green and that was it. :green_circle:
Can someone tell me what the error is and how to fix it?
Thank you

2 Likes

you shouldn’t add an instance as a key to a table like you did with
debounce[character] = true.

you should instead use table.insert(), and use table.find() to verify if it is in debounce like this:

--[[replace]] debounce[character] = true; --[[with]] table.insert(debounce, character)
--[[replace]] debounce[character] = false; --[[with]] debounce[table.find(debounce, character)
--[[replace]] and not debounce[character]; --[[with]] and not table.find(debounce, character)

tip:

instead of doing

if humanoid and not table.find() then 
-- rest of ur code

do:

if not humanoid or table.find() then return end
-- rest of ur code

it’s easier for readability and minimizes nesting

1 Like

instead of humanoid.Health = Humanoid.Health - 10 you could use Humanoid:TakeDamage(number) (i think this is how you write it)
as for why it doesn’t work, maybe it has something to do with the debounce table, altough i dont use table often so idrk

I made some changes there and it works now. And instead of color, there is a smoke.

local part = script.Parent
local debounce = {}

local function onTouch(hit)
	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	local smoke = part:FindFirstChild("Smoke")

	if humanoid and not debounce[character] then
		debounce[character] = true
		humanoid.WalkSpeed = humanoid.WalkSpeed - 10       
		smoke.Opacity = 0.2
		
		for i = 1, 10 do
			humanoid.Health = humanoid.Health - 10      
			task.wait(1)
		end
		
		task.wait(1)
		debounce[character] = nil
		smoke.Opacity = 4
		
		task.wait(10)          

		humanoid.WalkSpeed = humanoid.WalkSpeed + 10
	end
end

part.Touched:Connect(onTouch)