I am making an obby, and right now I have a ton of kill parts with individual kill scripts in each one with a simple part.touched event in each one. However things changed and now I want to just have one script that makes it so if a player ever touches a part that is named “Kill” or something like that they die.
You can place a Server Script in ServerScriptService and do
for index, killpart in pairs(game.Workspace:GetDescendant()) do
if killpart.Name == "Kill" then
killpart.Touched:Connect(function(hit)
--do your kill stuff
end)
end
end
(Edit: fixed a typo)
Don’t do this. It will lag the game because it is getting everything in the workspace, which can be over 1000+ things being looped over!
Try this instead.
local Players = game:GetService("Players")
local DamageName = "kill" -- Make sure the name is all lowercase or the script will break
local DamageAmount = math.huge
function PlayerAdded(Player)
local function CharacterAdded(Character)
for _,v in pairs(Character:GetChildren()) do
if v:IsA("Part") or v:IsA("MeshPart") then
v.Touched:Connect(function(hit)
if DamageName == string.lower(hit.Name) then
if Character:FindFirstChildOfClass("Humanoid") then
Character:FindFirstChildOfClass("Humanoid"):TakeDamage(DamageAmount)
end
task.wait(0.5)
end
end)
end
end
end
Player.CharacterAdded:Connect(CharacterAdded)
end
Players.PlayerAdded:Connect(PlayerAdded)
How would this work? You’re getting the children on the character.
But then when the player walks, the player touch so many things. It will also run the function for many times which causes lag.
Humanoid.Touched surprisingly is a thing. When any part parented to the humanoid model is touched, this event is fired. Also for convenience doing :IsA(“BasePart”) checks for all types of part.
This seems like a viable method tho.
When the children touch something, that part that they touch is being identified as ‘hit’.
Then, we check if ‘hits’ name is “kill”, if it is it damages them.
If you need to, you can test this.
It would. However, I added task.wait
and if you need to you can always add a debounce. Common fixes for common solutions!
The best way to do this:
local Kill = script.Parent
Kill.Touched:Connect(function(BasePart)
local Humanoid = BasePart.Parent.Humanoid
if game:GetService("Players"):GetPlayerFromCharacter(BasePart.Parent) then
Humanoid.Health = 0
end
end)
Place the script in each kill brick with my plugin:
https://www.roblox.com/library/refer/8612031162/EZ-Selection?Category=Plugins&SortType=Relevance&SortAggregation=AllTime&SearchKeyword=EZ%20Selection&CreatorId=0&Page=1&Position=1&SearchId=bdeb88c7-e1c4-4f99-84fd-936d5ddf12e8
Service = ServerStorage
Part = Kill
Just paste the script into the selected.
This is least laggy way to do this.
For some reason this doesn’t work. For more information it might be important to note that the map isn’t spawned in before the player joins. It is a round based obby game so the map is sitting in server storage until the round starts which is way after the player joins the game.
You should use my method, it minimizes the lag. it’s also the easiest
If you read my post that is actually what I already have but my game uses a lot of moving parts which caused huge lag so I decided to use local scripts to move the map into the workspace which greatky reduced lag. However the scripts didn’t work now because the server can’t recognize that they are in the workspace since a local script moved it in
How about you add a Heartbeat function beforehand?
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
humanoid.Touched:Connect(function(hitPart, humanoidPart)
if hitPart.Name == "" then --Change to name of kill part.
humanoid:TakeDamage(humanoid.MaxHealth)
end
end)
end)
end)
https://developer.roblox.com/en-us/api-reference/event/Humanoid/Touched