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

forcefield function doesnt work?

its suppose to be a script that gives you a permanent forcefield around your player so you can never die but it doesn't really work. :/

function newPlayer(player)
	local ff = Instance.new("ForceField")
	ff.Parent = player.Character.Torso
end

game.Players.PlayerAdded:connect(newPlayer)

2 answers

✓ Accepted answer

Force Fields don't go inside the player's torso. It only goes inside the character:

function newPlayer(player)
    local ff = Instance.new("ForceField")
    ff.Parent = player.Character
end

game.Players.PlayerAdded:connect(newPlayer)


Also, did you know that Instance.new() has 2 arguments? The object and the parent! So line 3 isn't needed.

function newPlayer(player)
    local ff = Instance.new("ForceField", player.Character) --Notice the "player.Character"
end

game.Players.PlayerAdded:connect(newPlayer)

EDIT

Like how the Jash50 said. Maybe the character hasn't been added yet because this script runs when the player joins the game. But when this happens the game isn't fully loaded for that player. Use this script to wait until the character is loaded.

game.Players.PlayerAdded:connect(function(plyr)
	plyr.CharacterAdded:connect(function(char) --Wait for the character to be added.
		local ff = Instance.new("ForceField", char)
	end)
end)

Also, if the player dies the character is added again so the function is triggered again. You don't need to have any Died event here.

3 comments

0

Okay then.

0

still doesn't work

0

I tried it and it still never worked

Maybe the character doesn't loaded yet.. so you can make it like this:

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		local ff = Instance.new("ForceField")
		ff.Parent = character.Torso
	end)
end)

Edit:

This should work:

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		local ff = Instance.new("ForceField", character)
	end)
end)

3 comments

0

It is in a regular script. If it were a local script, maybe that would be true, but local scripts can't use the PlayerAdded event.

0

It works in regular script because I tested it already xD. As you said, ff can't put in torso but can only be put in character. :)

0

What I'm trying to say is local scripts run before the game is even fully loaded.

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