Perhaps make sure that the object touching the part is an actual player, else any brick could touch it. ;)
function onTouched(Hit)
local Player = Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Humanoid.Health > 0 and game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
local MSG = Instance.new("Hint", game.Workspace)
wait()
MSG.Text="Worked"
wait(5)
MSG:Destroy()
end
end
script.Parent.Touched:connect(onTouched)
Debounce is a very useful concept to learn. It means that you can only process one chunk of executions at a time.
It's like regenerating a car (if you're an old ROBLOX player like me, you remember this ;)), the purple regenerating button turns black for a certain amount of time. Then becomes back to purple, allowing you to regenerate the car again.
The most common way of implementing this is to manipulate a static variable and wrap an "if" statement around your chunk of code.
Debounce = false
function onTouched(Hit)
local Player = Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Humanoid.Health > 0 and game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
if not Debounce then
Debounce = true
local MSG = Instance.new("Hint", game.Workspace)
wait()
MSG.Text="Worked"
wait(5)
MSG:Destroy()
Debounce = false
end
end
end
script.Parent.Touched:connect(onTouched)
1 comment
Please format your code in Lua code block.