Wallrun script doesn't run after death

Hi,

Self explanatory but my wallrun script doesn’t run after you die. I am not sure why but I tried debugging and I found something.


-- TODO: Raycasts
local function LeftWallCheck()
	return workspace:Raycast(Root.Position, -Root.CFrame.RightVector * RAY_LENGTH, RaycastParameters)
end

local function RightWallCheck()
	return workspace:Raycast(Root.Position, Root.CFrame.RightVector * RAY_LENGTH, RaycastParameters)
end

--[ Main Functions ]--

function Wallrun()	
	local LeftResult = LeftWallCheck()
	local RightResult = RightWallCheck()
	
	print("Wall run function was ran.")
	if not LeftResult and not RightResult then return end -- needs to go through these casts first.
	print("Passed through casts")

First thing I found was before I died, print(“Functions are ran.”) and print(“Passed through casts”) both ran and the wallrun worked. But after I died, the only print that worked was print(“Functions are ran.”). The other print (print(“Passed through casts”)) didn’t print anymore and the wallrun didn’t work anymore.

Check if your Root value updates to your new character Root after death.

1 Like

Before and after death it printed: Functions are ran: HumanoidRootPart


function Wallrun()	
	local LeftResult = LeftWallCheck()
	local RightResult = RightWallCheck()
	
	print("Functions are ran: ".. Root.Name)
18:42:33.503  Functions are ran: HumanoidRootPart  

second test:


function Wallrun()	
	local LeftResult = LeftWallCheck()
	local RightResult = RightWallCheck()
	
	print("Functions are ran: ".. tostring(Root))
18:45:58.478  Functions are ran: HumanoidRootPart

I’m unsure where this script is and whether it’s client or server so I’ll check off all the boxes:


StarterPlayerScripts (LocalScript):

StarterPlayerScripts will only run the LocalScript once.

Any further extrapolation requires custom functions. For example, if you did local character = game.Players.LocalPlayer.Character at the start of the LocalScript this is why the code breaks on death. The LocalScript will only ever store a reference to the first character, never any characters that are created afterward. You will need to update the reference to the character.

You can do this like so:

local PLR = game:GetService("Players")

local player = PLR.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

player.CharacterAdded:Connect(function(newCharacter)
	character = newCharacter
end)


StarterCharacterScripts (LocalScript):

StarterCharacterScripts will run the LocalScript every time a character is added.

You do not need to update the reference to the character because the script will be deleted/reset every time the character dies. You are most likely not using this approach since the HumanoidRootPart shouldn’t be ill-defined after death.



ServerScriptService / Workspace (Script):

ServerScriptService / Workspace will run the Script once.

Any further extrapolation requires custom functions.

You can do this:

local PLR = game:GetService("Players")

PLR.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		
	end)
end)

Hope this helps! :smile:

1 Like

This script was indeed inside StarterPlayerScripts. I did not know that starterplayerscripts ran once. Thank you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.