local Part = script.Parent
local debounce = false
function DamagePlayer(part)
if debounce then
return
end
if part.Parent:IsA("Accessory") then
return
end
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid then
debounce = true
humanoid.Health = humanoid.Health - 10
else
end
end
Part.Touched:Connect(DamagePlayer)
what i’ve noticed is that the “debounce” will make it so only one player can touch it any no body else will be able to. I know a way to fix that is to remove the “debounce” but i want it so it only fires once but can fire once per person.
ALSO if you can, whats a way to make the script only happen to the player?
for example, lets say you wanted to make a humanoid who touches a part have a GUI on their left side. how would you make it so it only happens to the player who touches the script?
Put this script in the part that damages the player:
script.Parent.Touched:Connect(function(touch)
if touch.Parent:FindFirstChild(“Humanoid”) then
touch.Parent.Humanoid.Health -= 10
end
end)
To make it fire once per person put a local script in the part and put this in the local script:
script.Parent.Touched:Connect(function(touch)
if touch.Parent:FindFirstChild(“Humanoid”) then
touch.Parent.Humanoid.Health -= 10
script.Parent.CanTouch = false
end
end)
local setPlayer
script.Parent.Touched:Connect(function(h)
local c = h.Parent.Name
local player = game:GetService('Players'):FindFirstChild(c)
if player then
setPlayer = player
end
end)
-- now the touchpart variable will be the player who touched it thus enabling you to do only whatever to that player through the variable.
Add a local script (or server/normal script it doesnt matter much for this kind of script) in the StarterCharacterScript
the code in the script should be like:
(if this is a local script and the player is R6 animation)
local Player = game.Players.LocalPlayer
local Torso = Player.Character.Torso
local once = false
Torso.Ontouched:connect(function(damage)
if once == false then
once = true
Player.Character.Humanoid.Health = Player.Character.Humanoid.Health - 1
end
end)
Responding to the original post, I often like to make a dictionary with different instances in this case. That way, each character has its own debounce to be checked and set.
local Part = script.Parent
local debounces = {}
function DamagePlayer(part)
if debounces[part.Parent] then
return
end
if part.Parent:IsA("Accessory") then
return
end
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid then
debounces[part.Parent] = true
humanoid.Health = humanoid.Health - 10
else
end
end
Part.Touched:Connect(DamagePlayer)