Mobile Sprint Button Breaking

Hello,

I have a mobile sprint button that breaks when the player dies. The UI is still there once the player respawns, but clicking the button won’t do anything. Furthermore, the code is still under the UI when the player resets, yet clicking the button won’t work still.

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")


script.Parent.MouseButton1Click:Connect(function()
	local WalkSpeed = 16
	local SprintSpeed = 24
	if script.Parent.ImageColor3 == Color3.new(0.392157, 0.392157, 0.392157) then
		script.Parent.ImageColor3 = Color3.new(0.580392, 0.580392, 0.580392)
	
		Humanoid.WalkSpeed = SprintSpeed
	else
		script.Parent.ImageColor3 = Color3.new(0.392157, 0.392157, 0.392157)
		
		Humanoid.WalkSpeed = WalkSpeed
	end		
end)

Is the GUI set to ResetOnSpawn?

If not, then whenever the player dies, they get a new character and your Character variable becomes outdated. In that case you could either set ResetOnSpawn to true, so a new script (and so, a new Character variable) would get instantiated. The second option is to make the Character and Humanoid update every time the character is added like so:

local Character = nil
local Humanoid = nil

local function InitCharacter(character)
    Character = character
    Humanoid = character:WaitForChild("Humanoid")
end

if player.Character then -- In case the character has loaded in already
    InitCharacter(player.Character)
end
player.CharacterAdded:Connect(InitCharacter) -- Update the variables every time a new character is added