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

Re - spawn Script problems?

I've been trying to test one of my local scripts that adds velocity to that player's torso, but I needed a way to re - create it after the player dies. All the script is supposed to do is add the required velocity back to the player's torso when he or she re - spawns:

function PlayerDied()
	print("began")
	repeat wait() until player.Character.Humanoid.Health ~= 0
	Body = Instance.new("BodyVelocity", player.Character.Torso)
	Body.Velocity = Vector3.new(-24,0,0)
	Body.MaxForce = Vector3.new(4000,0,4000)
	print("done")
end
player.Character.Humanoid.Died:connect(PlayerDied)

The problem is, the function only runs once for when the player dies. The script is local and is located in the StarterPlayerScripts file. NOTE the script MUST be local (UserInputService). Please HALP!!! Thx!

--------------EDIT--------------

Here's the whole script that I'm running just in case there might be some conflicting info:

Var = 0
Var1 = 0
player = game.Players.LocalPlayer

print(player)
repeat wait() until player.Character ~= nil
Body = Instance.new("BodyVelocity", player.Character.Torso)
Body.Velocity = Vector3.new(-24,0,0)
Body.MaxForce = Vector3.new(4000,0,4000)

function PlayerDied()
	print("began")
	repeat wait() until player.Character.Humanoid.Health ~= 0
	Body = Instance.new("BodyVelocity", player.Character.Torso)
	Body.Velocity = Vector3.new(-24,0,0)
	Body.MaxForce = Vector3.new(4000,0,4000)
	print("done")
end
player.Character.Humanoid.Died:connect(PlayerDied)

game:GetService("UserInputService").InputBegan:connect(function(Key, Condition)
	if player.Character.Humanoid.Health ~= 0 then
		if Key.KeyCode == Enum.KeyCode.D then
			Var = 1
			if Var == 1 and Var1 == 1 then
				Body.Velocity = Vector3.new(-24,0,0)
			elseif Var == 1 then
				Body.Velocity = Vector3.new(-24,0,-24)
			end
		end
		if Key.KeyCode == Enum.KeyCode.A then
			Var1 = 1
			if Var == 1 and Var1 == 1 then
				Body.Velocity = Vector3.new(-24,0,0)
			elseif Var1 == 1 then
				Body.Velocity = Vector3.new(-24,0,24)
			end
		end
	end
end)


game:GetService("UserInputService").InputEnded:connect(function(Key, Condition)
	if player.Character.Humanoid.Health ~= 0 then
		if Key.KeyCode == Enum.KeyCode.D then
			if Var == 1 and Var1 == 1 then
				Body.Velocity = Vector3.new(-24,0,24)
				Var = 0
			elseif Var == 1 and Var1 == 0 then
				Body.Velocity = Vector3.new(-24,0,0)
				Var = 0
			end
		end
		if Key.KeyCode == Enum.KeyCode.A then
			if Var == 1 and Var1 == 1 then
				Body.Velocity = Vector3.new(-24,0,-24)
				Var1 = 0
			elseif Var1 == 1 and Var == 0 then
				Body.Velocity = Vector3.new(-24,0,0)
				Var1 = 0
			end
		end
	end
end)

2 answers

✓ Accepted answer

##Problem

The script is only getting the first humanoid which you receive on spawn. It will not detect if you have respawned or not. Also, for efficiency sake, you would want to use the CharacterAdded event of Player. I would also recommend for the script to be in a Server Sided script, since technically I do not see the script properly being used by the client.


##Solution

For the revised script, I have added the PlayerAdded event to detect if a player has joined the server. I then included the CharacterAdded event for every time the player respawns. This way, when they respawn the new BodyVelocity object will be added to their character. These changes turned out to be script fixing changes rather than just for efficiency.


##Final Solution

game.Players.PlayerAdded:connect(function(Player) --Make a new function for the newly joined player. Name the new player "Player" as a variable in the script.
    Player.CharacterAdded:connect(function(Character) --Run a function for every time that player respawns. Name the spawning character model "Character"
        repeat wait() until Character and Character.Torso --Wait until Character and the torso are available.
            local BodyVelocity = Instance.new('Body Velocity', Character.Torso)
            BodyVelocity.Velocity = Vector3.new(-24,0,0)
            BodyVelocity.MaxForce = Vector3.new(4000,0,4000)
        end
    end)
end)

######If you have any questions, feel free to post below. If this answered your question do not forget to hit the Accept Answer button to the right.

6 comments

0

Actually... I'm sorry to say this but the script NEEDS to be local (UserInputService). Please edit your script to work around this. Sorry :/

0

Why does it NEED to be local? If that's the case, you can remove the PlayerAdded functions. But the Server Should be dealing with BodyObjects inside characters, not the client.

0

Your saying that you can use UserInputService inside a server - side script? If so, is this service part of a player instead of the game as a whole?

0

It specificly says this on the wiki: "The UserInputService is a service which is used to detect the type of input available on a user's device via the use of a LocalScript. It allows LocalScripts to perform different actions depending on the device used to provide the best experience for the end user" --Quote from http://wiki.roblox.com/index.php?title=API:Class/UserInputService

0

How was I to know that the script was utilizing UIS if you did not show any portion of the script that did utilize it, nor did you state it in your explanation? Like I said, just remove the PlayerAdded event.

0

Sorry about that bud. Though I'll accept your answer, but in the future when your answering questions only change the parts that need fixing. It gets REALLY annoying when people change your scripts up from local to server and vice - versa.

This problem has a simple fix: the StarterPlayerScripts folder only inserts the scripts when the player first joins in, not when they respawn. To fix it just put a script in your game that clones this script into their player in the players folder or into their palyergui or backpack. Hope this helps.

2 comments

0

Would it be possible to change the parent of the script to the PlayerScripts file?

0

Yes, you just have to specify what the script's parent will be.

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