Mobile buttons dont work after second spawn

hi guys!

i am working on a lightsaber game and i want to add mobile buttons.
the weird thing is tho the mobile buttons work when you first join but when you reset they dont work anymore?
this is an example of what i mean + it has no errors in the output:

here is the code used for the buttons:

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait() --if character hasnt loaded yet, wait

local function onclick()
	local Lightsaber = Char:FindFirstChild("Lightsaber IV Dual") --im assuming this is the tool
	if Lightsaber and Char then
		local EnableFolder = Lightsaber:WaitForChild("Enable/Disable") --assuming this is a folder
		local EnableEvent = EnableFolder:WaitForChild("EnableSaber")
		local sound = Lightsaber.lightsaberonidle
		if EnableEvent then
					EnableEvent:FireServer()
		sound:Play()
		script.Parent.Parent.Parent.Visible = false
		script.Parent.Parent.Parent.Parent.WhenEnabled.Visible = true
		script.Parent.Ignite:Play()
		end

	end
end

script.Parent.MouseButton1Click:connect(onclick)

please help me with this!!

Based on the code provided, it seems that the issue might be related to the event listener not being properly reconnected after a reset. When you reset the game, the script is likely being reloaded, but the event listener for the button click is not being reconnected.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()

local function onClick()
    local Lightsaber = Char:FindFirstChild("Lightsaber IV Dual")
    if Lightsaber and Char then
        local EnableFolder = Lightsaber:WaitForChild("Enable/Disable")
        local EnableEvent = EnableFolder:WaitForChild("EnableSaber")
        local sound = Lightsaber.lightsaberonidle
        if EnableEvent then
            EnableEvent:FireServer()
            sound:Play()
            script.Parent.Parent.Parent.Visible = false
            script.Parent.Parent.Parent.Parent.WhenEnabled.Visible = true
            script.Parent.Ignite:Play()
        end
    end
end

script.Parent.MouseButton1Click:Connect(onClick)

Player.CharacterAdded:Connect(function(character)
    Char = character
end)
1 Like

this is just what i needed XD TYYY

1 Like

You’re welcome, if you need something contact me!

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