Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

why wont my explosion go to my position,did I not get the character correctly?

game.Players.PlayerAdded:Connect(function(player)
local char = player.Character	
wait(5)
local exp = Instance.new("Explosion", char)	
exp.ExplosionType = Enum.ExplosionType.Craters
exp.Position = char.Position
		
end)

I made a normal script were when the player joins I want a explosion to be at there position i'm not sure what i'm doing wrong.On the last line(6) it says "attempt to index nil with 'position' " even though I defined what char means unless I did it wrong.

2 answers

✓ Accepted answer

The character does not have a position because it is a model. However, the descendants of the character has positions. For example, HumanoidRootPart and Torso.

To call the positions, just simply write whatever you're trying to get the position of. char.HumanoidRootPart.Position

However, there is also another problem that occurs. The character might not be loaded when the player joins the game. So therefore you cannot just simply write local char = player.Character. You could wait for the character to be added by using the player.CharacterAdded event. To wait for the character, just simply write player.CharacterAdded:Wait() between line 1 and line 2. Or, if you wanted the character to explode every time they load in (both joining and respawning), write something like this:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		--code
	end)
end) 

Also, the descendants of the character may not be loaded when the character loads in. So just add another wait to counter that. For example, local hrp = character:WaitForChild("HumanoidRootPart")

I hope this helps!

1 comment

0

thank you this actually explains it better for me to understand as well

game.Players.PlayerAdded:Connect(function(player)	
	player.CharacterAdded:Connect(function(character)	
	wait(5)
	local exp = Instance.new("Explosion", character)
	local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
	exp.ExplosionType = Enum.ExplosionType.Craters
	exp.Position = HumanoidRootPart.Position
	end)
end)

You need to get character through the player.CharacterAdded function and you never actually looked for an object when you tried to position the explosion, this should work.

1 comment

0

thanks for this answer

This discussion is preserved as it was. New answers, comments, and votes are closed.