Controlscript stops working after a player respawns

Hello again fellow robloxians!

So, I want to know about why this controlscript that I developed wont work after a player respawns?

I put the controlscript code in a modulescript, and I have made a enable and disable feature for it, so whenever a player joins, a local script calls for the enable function.

To furthermore explain the problem, I have provided a video and both codes down below on what I am talking about.

Problem vid (if you cant download it lmk.)

robloxapp-20230420-1624226.wmv (411.7 KB)

!!! ALL CODES BELOW ARE INSIDE OF STARTERPLAYERSCRIPTS (dont know if this matters but inside of starterplayer, in controls, in devcomputermovementmode its set to scriptable.) !!!

Codes

control script enabler (localscript)

local controllermodule = require(script["Gamepads/Keyboard"])
local plr = game:GetService("Players").LocalPlayer

local function CharacterAdded(char)
	controllermodule:EnableController()
end

local function PlayerAdded(player)
	player.CharacterAdded:Connect(CharacterAdded)

	local char = player.Character
	if char then
		CharacterAdded(char)
	end
end

game.Players.PlayerAdded:Connect(PlayerAdded)

for i,v in pairs(game.Players:GetPlayers()) do
	PlayerAdded(v)
end

controlscript (modulescript)

local controller_module = {}

local player = game:GetService("Players").LocalPlayer
repeat task.wait() until player.Character

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

local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local jumping = false

local leftValue, rightValue = 0, 0

local moveDirection

function controller_module.EnableController (self)
	self = controller_module
	
	local function onLeft(actionName, inputState)
		if inputState == Enum.UserInputState.Begin then
			leftValue = 1
		elseif inputState == Enum.UserInputState.End then
			leftValue = 0
		end
	end

	local function onRight(actionName, inputState)
		if inputState == Enum.UserInputState.Begin then
			rightValue = 1
		elseif inputState == Enum.UserInputState.End then
			rightValue = 0
		end
	end

	local function onJump(actionName, inputState)
			if inputState == Enum.UserInputState.Begin then
				jumping = true
			elseif inputState == Enum.UserInputState.End then
				jumping = false
			end
	end

	local function onUpdate()
		if character and character:FindFirstChild("Humanoid") then
			if jumping then
				character:WaitForChild("Humanoid").Jump = true
			end
			moveDirection = rightValue - leftValue
			character:WaitForChild("Humanoid"):Move(Vector3.new(moveDirection,0,0), false)
		end
	end

	RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)

	ContextActionService:BindAction("Left", onLeft, true, Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
	ContextActionService:BindAction("Right", onRight, true, Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
	ContextActionService:BindAction("Jump", onJump, false, Enum.KeyCode.Space, Enum.KeyCode.ButtonA)
	
	return self
end

function controller_module.DisableController (self)
	self = controller_module

	RunService:UnbindFromRenderStep("Control")
	
	ContextActionService:UnbindAction("Left")
	ContextActionService:UnbindAction("Right")
	ContextActionService:UnbindAction("Jump")
		
	character:WaitForChild("Humanoid"):Move(Vector3.new(0,0,0), false)

	return self
end

return controller_module

To conclude, may I at least some advice here to lead me to my destination? Basically, I want the controlscript to work when you respawn and so on and so fourth. Thank you for the help.

Don’t see any reason for this bit. Try removing it and see what happens.

1 Like

Is the script in starterplayer or startercharacter?

which one are you talking about sir? there are two of them, the module script is inside of the local script, and the local script is inside of starterplayerscripts

edit: this is for a 2d game btw

Im talking about the local script,
And since its in starter player onfe the character dies, aka respawns the character value becomes nil because the script doesnt reset

Try pittinf the local script in starter character so when the player respawns, so does the script

I have done that before, and it does not work. I have to replace the already existing controlscript that roblox gives you by default in order for the controlscript to work, so it has to be in starterplayerscripts.

can you send me a copy of the game so i can do some testing?

here you go, hope all is well.

stw.rbxl (47.8 KB)

ok i found the issue, i was correct, the character variable in the module script is set and stone so when the character dies the variable basically is referencing the old character, not the new one that is spawned after death.

I found a couple of fixes.

  1. Move the script to StarterCharacter Scripts and and this code instead

    local controllermodule = require(script:WaitForChild("Gamepads/Keyboard"))
    

    the WaitForChild is important if you were to move it to starterCharater

  2. You can update the character variable every OnUpdate()

  3. You can do this inside the module instead

    repeat task.wait() until player.Character
      
    local character = player.Character or player.CharacterAdded:Wait()
      
    local RunService = game:GetService("RunService")
      
    local ContextActionService = game:GetService("ContextActionService")
      
    local jumping = false
      
    local leftValue, rightValue = 0, 0
      
    local moveDirection
      
    player.CharacterAdded:Connect(function(x)
      
    character = x
      
    end)
    

any one of these should fix it

i apologize for the late reply, but where would i put the player.characteradded function?

in the module script as i showed in number 3

i know what your talking about, im talking about where in the script, like the sequence of it.

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

where would this line of code go is what im trying to say

what do you mean? I sent the code, you can really put it anywhere in the script but i reccomend just put it after the moveDirection Variable

So would I combine both of the codes above and below of me?, thats where im gettin confused at. If im still being a little confusing, do you have the edited place file, if so can you send it to me then? Thank you.

function controller_module.EnableController (self)
	self = controller_module
	
	local function onLeft(actionName, inputState)
		if inputState == Enum.UserInputState.Begin then
			leftValue = 1
		elseif inputState == Enum.UserInputState.End then
			leftValue = 0
		end
	end

	local function onRight(actionName, inputState)
		if inputState == Enum.UserInputState.Begin then
			rightValue = 1
		elseif inputState == Enum.UserInputState.End then
			rightValue = 0
		end
	end

	local function onJump(actionName, inputState)
			if inputState == Enum.UserInputState.Begin then
				jumping = true
			elseif inputState == Enum.UserInputState.End then
				jumping = false
			end
	end

	local function onUpdate()
		if character and character:FindFirstChild("Humanoid") then
			if jumping then
				character:WaitForChild("Humanoid").Jump = true
			end
			moveDirection = rightValue - leftValue
			character:WaitForChild("Humanoid"):Move(Vector3.new(moveDirection,0,0), false)
		end
	end

	RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)

	ContextActionService:BindAction("Left", onLeft, true, Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
	ContextActionService:BindAction("Right", onRight, true, Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
	ContextActionService:BindAction("Jump", onJump, false, Enum.KeyCode.Space, Enum.KeyCode.ButtonA)
	
	return self
end

function controller_module.DisableController (self)
	self = controller_module

	RunService:UnbindFromRenderStep("Control")
	
	ContextActionService:UnbindAction("Left")
	ContextActionService:UnbindAction("Right")
	ContextActionService:UnbindAction("Jump")
		
	character:WaitForChild("Humanoid"):Move(Vector3.new(0,0,0), false)

	return self
end

Here just copy paste this exact code into the module script

local controller_module = {}

local player = game:GetService("Players").LocalPlayer
repeat task.wait() until player.Character

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

local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local jumping = false

local leftValue, rightValue = 0, 0

local moveDirection

player.CharacterAdded:Connect(function(x)
	character = x
end)
function controller_module.EnableController (self)
	self = controller_module
	local function onLeft(actionName, inputState)
		if inputState == Enum.UserInputState.Begin then
			leftValue = 1
		elseif inputState == Enum.UserInputState.End then
			leftValue = 0
		end
	end

	local function onRight(actionName, inputState)
		if inputState == Enum.UserInputState.Begin then
			rightValue = 1
		elseif inputState == Enum.UserInputState.End then
			rightValue = 0
		end
	end

	local function onJump(actionName, inputState)
			if inputState == Enum.UserInputState.Begin then
				jumping = true
			elseif inputState == Enum.UserInputState.End then
				jumping = false
			end
	end

	local function onUpdate()
		if character and character:FindFirstChild("Humanoid") then
			
			if jumping then
				character:WaitForChild("Humanoid").Jump = true
			end
			moveDirection = rightValue - leftValue
			character:WaitForChild("Humanoid"):Move(Vector3.new(moveDirection,0,0), false)
		end
	end

	RunService:BindToRenderStep("Control", Enum.RenderPriority.Input.Value, onUpdate)

	ContextActionService:BindAction("Left", onLeft, true, Enum.KeyCode.Left, Enum.KeyCode.DPadLeft)
	ContextActionService:BindAction("Right", onRight, true, Enum.KeyCode.Right, Enum.KeyCode.DPadRight)
	ContextActionService:BindAction("Jump", onJump, false, Enum.KeyCode.Space, Enum.KeyCode.ButtonA)
	
	return self
end

function controller_module.DisableController (self)
	self = controller_module

	RunService:UnbindFromRenderStep("Control")
	
	ContextActionService:UnbindAction("Left")
	ContextActionService:UnbindAction("Right")
	ContextActionService:UnbindAction("Jump")
		
	character:WaitForChild("Humanoid"):Move(Vector3.new(0,0,0), false)

	return self
end

return controller_module

1 Like

It works! Thank you very much.

Not to take much more of your time, I am just a little curious here…

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

How did you even figure it out??

I wrote my own movement machine once and just experienced this exact issue thats it :]

1 Like

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