Hey there, so currently, I am trying to make a script that if a specific part in workspace is touched, then my character or model of my avatar explodes. But it does not work, no errors, debugging does not work. Could anybody help me?
Script (global):
local player = game.Players.VakarisLDoE
local block = game.Workspace.Food
block.Touched:Connect(function()
if player.Character ~= nil then
player.Character:Destroy()
print("worked")
end
end)
You’re running into an issue where your player isn’t in the server yet when you start a playtest, so the server can’t do anything. You will have to wait for your player to load in, so you will need to do this
local player = game.Players:WaitForChild("VakarisLDoE")
instead of
local player = game.Players.VakarisLDoE
Alternatively, you can use this to destroy the character of whoever touches the part.
local players = game:GetService("Players")
local block = game.Workspace.Part
block.Touched:Connect(function(hit)
if hit.Parent:WaitForChild("Humanoid") ~= nil then
local plr = players:GetPlayerFromCharacter(hit.Parent)
if plr ~= nil then
plr.Character:Destroy()
end
end
end)
I just expanded on the script you provided to make it work. I’ll see what I can do on making the character explode, using this script as a base:
local players = game:GetService("Players")
local block = game.Workspace.Part
block.Touched:Connect(function(hit)
if hit.Parent:WaitForChild("Humanoid") ~= nil then
local plr = players:GetPlayerFromCharacter(hit.Parent)
if plr ~= nil then
plr.Character:Destroy()
end
end
end)
:Destroy() deletes the instance it was called on.
You want to make a new explosion instance, set its position to the player’s position, and parent it to workspace.
local players = game:GetService("Players")
local block = game.Workspace.Part
block.Touched:Connect(function(hit)
if hit.Parent:WaitForChild("Humanoid") ~= nil then
local plr = players:GetPlayerFromCharacter(hit.Parent)
if plr ~= nil and plr.Character:WaitForChild("Humanoid").Health ~= 0 then
local explosion = Instance.new("Explosion")
explosion.Position = plr.Character.HumanoidRootPart.Position
explosion.Parent = workspace
end
end
end)
That means the function is most likely going to yield infinitely since it won’t be able to find the Humanoid object in Workspace. you can add a timeout number to :WaitForChild() by putting a “, 5” after the instance that its looking for.
hit.Parent:WaitForChild("Humanoid", 5)
This will make it stop looking for the Humanoid after 5 seconds.
Yes, basically. The timeout part is optional if you want to wait until the desired child is found. Otherwise, just set it so that it eventually stops looking for it after the desired amount of time.
Roblox documentation also states that if the timeout parameter is not set, and it has been more than 5 seconds since it was used, it will print out that warning you received. Otherwise, if the parameter has been set, it will not send that warning out either.
As far as I can tell, I think the performance impact is negligible? It just stops the script or function from progressing until it finds what is being looked for, so I assume there really isn’t much of an impact unless you really spam it.