When a parts parent is the workspace i want it to kill (preferably explode) everyone
I've tried this
if script.Parent.Parent == workspace then game:GetChildren("Humanoid").Health = 0 end
it probably looks very wrong but i tried it and i dont have any other solutions
Simply getting all players and setting their health to 0
for i,v in pairs(game.Players:GetPlayers()) do v.Character.Humanoid.Health = 0 end
I'm not sure why you'd want a part to kill when it enters the workspace, but it is a little more complicated than that.
First, you need to have an event listener which waits for the part to enter the workspace. You can use ancestrychanged for this:
https://developer.roblox.com/en-us/api-reference/event/Instance/AncestryChanged
Second, :GetChildren() is a function that returns an array of all chidren to the instance it was called on. It cannot scan for specific types, nor does it return anything more than immediate children. As a result, calling :GetChildren() on game returns high level services like the workspace and serverscriptservice.
To amend this issue, you need to find all players first. You can do this easily by doing game.Players:GetChildren(), which returns all of the children to the Players service. This is not enough however, because you need to set all of their character humanoids health to 0. That is when a loop comes in.
To iterate over an array, you can use a for loop like this:
for i,v in pairs (array) do --do stuff end
i
will represent the index (or number) in the array, while v
is the entry in the array itself.
Using a for loop over all players, you can set v.Character.Humanoid.Health=0
.
If you need more help, let me know.
You should find the character of the player and set their health to 0 Also fire a remote event to all clients.
Server script:
if part.Parent == workspace then game.ReplicatedStorage.RandomEventName:FireAllClients(0) end
local script:
game.ReplicatedStorage.RandomEventName.OnClientEvent:Connect(function(newHealth) game.Players.LocalPlayer.Character:WaitForChild("Humanoid").Health = newHealth end)