Why is my perfectly normal LocalScript not running?

So, my script is supposed to toggle the player’s AutoJumpEnabled boolean if they press the button, but when I test the button, nothing happens when I press it even though there aren’t any errors in the script nor the console.

local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function(character)
	script.Parent.MouseButton1Click:Connect(function()
		local humanoid = character.Humanoid

		if humanoid.AutoJumpEnabled == true then
			humanoid.AutoJumpEnabled = false
			script.Parent.BackgroundColor3 = Color3.fromRGB(227, 0, 61)
		elseif humanoid.AutoJumpEnabled == false then
			humanoid.AutoJumpEnabled = true
			script.Parent.BackgroundColor3 = Color3.fromRGB(0, 170, 127)
		end
	end)
end)

I don’t understand at all why it’s not working. The object hierarchy doesn’t seem strange either.

heirarchy

2 Likes

Get rid of the player.CharacterAdded. It’s connecting a new button event every time you respawn.

Code:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	local humanoid = player.Character.Humanoid

	if humanoid.AutoJumpEnabled then
		humanoid.AutoJumpEnabled = false
		script.Parent.BackgroundColor3 = Color3.fromRGB(227, 0, 61)
	else
		humanoid.AutoJumpEnabled = true
		script.Parent.BackgroundColor3 = Color3.fromRGB(0, 170, 127)
	end
end)

Shortened version if you prefer it:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	local humanoid = player.Character.Humanoid
	humanoid.AutoJumpEnabled = not humanoid.AutoJumpEnabled
	script.Parent.BackgroundColor3 = humanoid.AutoJumpEnabled and Color3.fromRGB(0, 170, 127) or Color3.fromRGB(227, 0, 61)
end)
4 Likes

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