Hi guys, I am making a sort of obby which has a lot of kill parts, I want to make one script for all of them, is there a event for when a player touches an object, also how could I get that object’s name?
script.Parent.Touched:Connect(function(Object)
if Object.Parent:FindFirstChild("Humanoid") then
Object.Parent.Humanoid.Health = 0
local Player = game.Players:GetPlayerFromCharacter(Object.Parent)
if Player then
print(Player.Name.."was killed by touching a brick called"..script.Parent.Name)
end
end
end)
You don’t need to get the objects name when a player touches a part, simply make a folder of the kill parts, go through that using :GetChildren() and do the rest from there. Ex:
for i,v in pairs(workspace.KillParts:GetChildren()) do
v.Touched:Connect(function(hit)
if hit.Parent:GindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health = 0
end
end
end
[IF IT’S FOR 1 PART], place it under the part you want the player to be killed on touching.
Thanks for that, but I wanted it for all the parts, I don’t think your script would work that way
local parts = game.Workspace.Killing:GetChildren()
for i,v in pairs(parts) do
v.Touched:Connect(function(Object)
if Object.Parent:FindFirstChild("Humanoid") then
Object.Parent.Humanoid.Health = 0
local Player = game.Players:GetPlayerFromCharacter(Object.Parent)
if Player then
print(Player.Name.."was killed by touching a brick called"..v.Name)
end
end
end)
end
Would this script work in ServerScriptService?
Putting A normal Script
in Workspace
will do the same thing, if you’d put it in ServerScriptService
[You’ll just need to change the variables accordingly]
It would work anywhere honestly, although preferably putting it in ServerScriptService as to organize where everything goes
Okay, thank you guys
And also is there no event to detect when a player touches something instead of something touching the player
That’s the same thing. When a part is interacting/touching a part, it’s still true to say the Player Touched the part.
Well nevermind, thank you anyways
You could use a Touched function on all of the players characters parts, but it’s much easier to do it this way instead