Hello I am currently making a game and need to make it so people take damage when walking through a barbed wire barricade, I currently have 0 clue as to how and was wondering if someone could help me.
The damage would only be on the wire mesh/part and would also be non-collidable.
Realistically speaking people shouldn’t just write out code for you since it’s lazy to just ask people to do stuff for you, especially in the situation where it’s a pretty common topic, but here’s the script.
-- Put the script in the barbed wire model
local part = script.Parent.Damage
local damage = 30 -- damage amount
local slow = 10 -- slow amount
local recentlyHit = {}
part.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local id = player.UserId
if recentlyHit[id] then return end
recentlyHit[id] = true
hum:TakeDamage(damage)
hum.WalkSpeed -= slow
task.wait(1)
hum.WalkSpeed = 16
recentlyHit[id] = nil
end
end)
local part = script.Parent
local dmg = 5
local function onTouch(otherPart)
if otherPart.Parent:IsA("Model")
otherPart.Parent:WaitForChild("Humanoid"):TakeDamage(dmg)
end
end
part.Touched:Connect(onTouch)
I feel like there’s a lot of issues with this script, being that
1 - Just because an instances parent is a model doesn’t mean its specifically the player
2 - There’s no end to the if statement
3 - It will infinitely yield if Humanoid isn’t found