Put them in StarterPlayerScripts and reference part with workspace.Part
@Amine360YT I tried using your script in roblox studio, attached to a part and it worked for me? Might be a problem on your end
local killPart = script.Parent;
killPart.Touched:Connect(function(Part)
if Part.Parent:FindFirstChild("Humanoid") ~= nil then
Part.Parent.Humanoid.Health = 0;
end
end)
@Feedekaiser , I tried your solution and it actually worked!
Here is the script inside the StarterPlayerScripts:
local killPart = game.Workspace.KillPart
killPart.Touched:Connect(function(hit)
print("Part has beed touched")
local char = hit.Parent -- Character
local hum = char:FindFirstChild("Humanoid") -- Humanoid
if hum then -- If humanoid then...
if hum.Health ~= 0 then -- Makes sure that character is not dead
hum.Health = 0 -- Kills the character
end
end
end)
Thanks for everyone replied to this topic!
local killPart = script.Parent
local function onPartTouch(hit)
local hum = hit.Parent.Humanoid
if hum then
hum.Health = 0
end
print("Part has been touched!")
end
killPart.Touched:Connect(onPartTouch)
I made a slightly simpler version.
Ok so
After looking at this I found a good idea of what you could’ve try
Note I didnt make the script
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:BreakJoints()
end
end)
But you could make it do a loop by doing this
script.Parent.Touched:Connect(function(hit)
While true do
Wait()
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:BreakJoints()
end
end
end)
Putting the script for controlling it, in starterplayerscripts has a chance to get exploited
if you index an empty field or nonexistent child it will error, therefore it makes no sense to check if hum
exist at all.
Should not be neccessary. Nor should this system be a local script as well. Having this in serverscript service as a server script should be fine. Though server scripts can run in workspace as well so that is not the problem. I would recomend using functions such as break joints to make sure you actually kill the player. If the script is a server script and is located either in server script service or the workspace this script should work fine though
This is a bad idea as it creates a new loop every time the part is hit, not to mention it doesn’t yield at all, which will crash roblox.
Fair enough but yours wont work either, he will have to make a nee script every time a nee one is created, due to the fact that if the parts are the same name roblox will not be able to find the actual one yout referenceing
No
if you have multiple killparts, consider using something like this
game.Players.LocalPlayer.CharacterAdded:Connect(funcition(character)
character:WaitForChild'Humanoid'.Touched:Connect(function(x)
if x.Name == 'KillPart' then
character:BreakJoints()
end
end)
end)
true, it’s pretty inefficient, i didn’t make it too good lol
Can We all Stop Our Suggestions on This Topic? We all are Fighting for No Reason.
The Issue Has Already Been Solved.
@Amine360YT you choose which ever one you want do choose but just know there were more people here saying you should have referenced the part by making the part the scripts parent
Hello everyone.
I tried a new script:
local killPartFolder = game.Workspace.KillParts
local killParts = killPartFolder:GetChildren()
for i, v in pairs(killParts) do
v.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid.Health = 0
print("The player has died")
end
end)
end
Is it a good idea?
Ok so I need to explain this a bit in depth
This is OK but it creates lots of connections.
And it will kill other players locally which is weird soooo
Does the script work? Also, using a for i , v in pairs loop is much more efficient than putting kill scripts in multiple parts.