Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My script stops working when the player respawns/dies?

Asked by
slatvr 0
3 years ago

--|| SERVICES ||-- local RunService = game:GetService("RunService"); local Players = game:GetService("Players"); --|| VARIABLES ||-- local LeftRightConnections = { }; --|| HELPER FUNCTIONS ||-- local function Lerp(a, b, c) return a + (b - a) * c; end --|| PLAYER ADDED AND REMOVED EVENTS ||-- game.Players.PlayerAdded:Connect(function(Player) local Character = Player.Character or Player.CharacterAdded:Wait(); local Humanoid = Character:WaitForChild("Humanoid"); local HumanoidRootPart = Character.HumanoidRootPart; local Torso = Character.Torso; local RootJoint = HumanoidRootPart.RootJoint; local LeftHipJoint = Torso["Left Hip"]; local RightHipJoint = Torso["Right Hip"]; local Force = nil; local Direction = nil; local Value1 = 0; local Value2 = 0; local RootJointC0 = RootJoint.C0; local LeftHipJointC0 = LeftHipJoint.C0; local RightHipJointC0 = RightHipJoint.C0; LeftRightConnections[Player] = RunService.Stepped:Connect(function() Force = HumanoidRootPart.Velocity * Vector3.new(1, 0, 1); if Force.Magnitude > 2 then Direction = Force.Unit Value1 = HumanoidRootPart.CFrame.RightVector:Dot(Direction); Value2 = HumanoidRootPart.CFrame.LookVector:Dot(Direction); else Value1 = 0; Value2 = 0; end; RootJoint.C0 = RootJoint.C0:Lerp(RootJointC0 * CFrame.Angles(math.rad(Value2 * 10), math.rad(-Value1 * 10), 0), 0.2); LeftHipJoint.C0 = LeftHipJoint.C0:Lerp(LeftHipJointC0 * CFrame.Angles(math.rad(Value1 * 10), 0, 0), 0.2); RightHipJoint.C0 = RightHipJoint.C0:Lerp(RightHipJointC0 * CFrame.Angles(math.rad(-Value1 * 10), 0, 0), 0.2); end); end) Players.PlayerRemoving:Connect(function(Player) if LeftRightConnections[Player] then LeftRightConnections[Player]:Disconnect(); LeftRightConnections[Player] = nil; end; end)

How do I make it so it continues to work when the player respawns? (its a server script just fyi)

1 answer

Log in to vote
1
Answered by 3 years ago

You need to wrap the code in your connection to Players.PlayerAdded in a connection to Player.CharacterAdded as Player.CharacterAdded:Wait() is not enough in this situation, since the function connected to Players.PlayerAdded is only ran once for each player that joins (meaning Player.CharacterAdded:Wait() is only evaluated once for each player), however Player.CharacterAdded:Connect(function() ... end) will consistently fire every time their character is regenerated after they die.

You should also add a connection to Humanoid.Died so that you can disconnect from RunService.Stepped upon the player's death to prevent a memory leak (and also to prevent the script from erroring, since HumanoidRootPart will be destroyed when their model respawns)

Doc Links: Player.CharacterAdded, Humanoid.Died

0
Thank you sm! slatvr 0 — 3y
0
Hey I came back to this because last time I read this it worked but all of the sudden I'm kinda confused on this could you tell me how to do this again? slatvr 0 — 3y
0
sorry for the extremely late response, i don't use this site often. essentially, if you want to ensure a function you want to execute every time a player's character spawns, you want to connect to Player.CharacterAdded instead of doing a :Wait, as when a player joins in Players.PlayerAdded, if you do :Wait, it will only be evaluated (and therefore run) once BrimoireGrimoire 11 — 3y
Ad

Answer this question