Players character not destroying

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)
1 Like
local player = game.Players:WaitForChild("VakarisLDoE")

So I don’t need to use this to destroy the character of whoever touched the block?

Also, another issue. My character just disappears. It has to explode, maybe it’s a glitch or something?

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)
1 Like

Ye, but doesn’t the “:Destroy()” makes your character explode or is there a different way and I am just dumb?

: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.

Roblox Documentation for :Destroy()

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)
1 Like

It works, but there is an interesting message in the output, do you know what this could mean?

output: (highlighted in orange)

Infinite yield possible on 'Workspace:WaitForChild("Humanoid")'

image

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.

1 Like

Oh, so the message is saying, that the function is looking for the Humanoid for an infinite amount of time?

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.

Roblox Documentation | :WaitForChild()

1 Like

I guess just leaving it without a timeout would be unoptimized right? After a bunch of these functions your game might lag, right?

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.

1 Like

Oh, ok. Thanks for helping me though! :+1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.