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