Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't this GUI work and also it just keeps repeating ?

local cando = true
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local oGUI = script:FindFirstChild("ScreenGui")
local db = false


tool.Equipped:Connect(function(mouse)
    local GUI = oGUI:Clone()
    GUI.Parent = player.PlayerGui
    wait(1)
    GUI.Equiping.Visible = false
    mouse.Button1Down:connect(function()
        if not db == true then
            db = true
            local ray = Ray.new(tool.Glowstone.CFrame.p, (mouse.Hit.p - tool.Glowstone.CFrame.p).unit * 400)
            local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
            local beam = Instance.new("Part",workspace)
            beam.FormFactor = "Custom"
            beam.Material = "Ice"
            beam.BrickColor = BrickColor.new("Cyan")
            beam.CanCollide = false
            beam.Anchored = true
            beam.Transparency = 0.15
            beam.Locked = true
            local distance = (tool.Glowstone.CFrame.p - position).magnitude
            beam.Size = Vector3.new(0.3 , 0.3 , distance)
            beam.CFrame = CFrame.new(tool.Glowstone.CFrame.p, position) * CFrame.new(0,0, -distance /2)
            game:GetService("Debris"):AddItem(beam, 0.1)
            if part then
                local human = part.Parent:FindFirstChild("Humanoid")
                if not human then
                    human = part.Parent.Parent:FindFirstChild("Humanoid")
                end
                if human then
                   human:TakeDamage(3)
                   human.WalkSpeed = 1
                end
            end
        else
           if cando then
	               cando = false
                   mouse.Button1Up:Connect(function()
	               GUI.Time.Visible = true
	               wait(1)
                   GUI.Time.Text = "4"
                   wait(1)
                   GUI.Time.Text = "3"
                   wait(1)
                   GUI.Time.Text = "2"
                   wait(1)
                   GUI.Time.Text = "1"
                   wait(1)
                   GUI.Time.Visible = false
                cando = true
                db = false
                         end)
             end
           end
    end)
end)

The GUI Time part doesn't work , and when you keep clicking it the GUI just keeps repeating even tho i added a debonce . THIS IS A LOCAL SCRIPT

1 answer

✓ Accepted answer

Looks like you don't really understand how events are working. "Connect" connects a function to an event. The function (usually people call it an event listener) will run every time an event happens, and it will run parallel to main script and all other event listeners. These event listeners are not synced with eachother. That's why your debounce doesn't work. You should put it inside the function.

mouse.Button1Up:Connect(function()
	if cando and db then
		cando = false
		GUI.Time.Visible = true
		wait(1)
		GUI.Time.Text = "4"
		wait(1)
		GUI.Time.Text = "3"
		wait(1)
		GUI.Time.Text = "2"
		wait(1)
		GUI.Time.Text = "1"
		wait(1)
		GUI.Time.Visible = false
		cando = true
		db = false
	end
end)

The second problem is that it is possible to attach multiple functions to a single event type, and you are creating a listener (the buttondown one) every time you equip the tool, and this listener creates another one every time you press a button. If you play for too long you'll end up with a lot of listeners processing every your click. This will lead to a memory leak and performance issues. Make sure that you create them once or destroy them afterwards.

local cando = true
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local oGUI = script:FindFirstChild("ScreenGui")
local db = false

--save the listeners to destroy them later
local shoot_listener, reload_listener

tool.Equipped:Connect(function(mouse)
    local GUI = oGUI:Clone()
    GUI.Parent = player.PlayerGui
    wait(1)
    GUI.Equiping.Visible = false
    shoot_listener = mouse.Button1Down:connect(function()
        if not db == true then
            db = true
            local ray = Ray.new(tool.Glowstone.CFrame.p, (mouse.Hit.p - tool.Glowstone.CFrame.p).unit * 400)
            local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
            local beam = Instance.new("Part",workspace)
            beam.FormFactor = "Custom"
            beam.Material = "Ice"
            beam.BrickColor = BrickColor.new("Cyan")
            beam.CanCollide = false
            beam.Anchored = true
            beam.Transparency = 0.15
            beam.Locked = true
            local distance = (tool.Glowstone.CFrame.p - position).magnitude
            beam.Size = Vector3.new(0.3 , 0.3 , distance)
            beam.CFrame = CFrame.new(tool.Glowstone.CFrame.p, position) * CFrame.new(0,0, -distance /2)
            game:GetService("Debris"):AddItem(beam, 0.1)
            if part then
                local human = part.Parent:FindFirstChild("Humanoid")
                if not human then
                    human = part.Parent.Parent:FindFirstChild("Humanoid")
                end
                if human then
                   human:TakeDamage(3)
                   human.WalkSpeed = 1
                end
            end
             end
           end
    end)

	reload_listener = mouse.Button1Up:Connect(function()
		if cando and db then
			cando = false
			GUI.Time.Visible = true
			wait(1)
			GUI.Time.Text = "4"
			wait(1)
			GUI.Time.Text = "3"
			wait(1)
			GUI.Time.Text = "2"
			wait(1)
			GUI.Time.Text = "1"
			wait(1)
			GUI.Time.Visible = false
			cando = true
			db = false
		end
	end)
end)

tool.Unequipped:Connect(function()
	--destroy the listeners
	shoot_listener:disconnect()
	reload_listener:disconnect()
end)

2 comments

0

Also there is no need for 2 listeners at all. You can put the code of the mouseUp listener in the end of mouseDown listener code and it will work.

0

Thanks gamer

This discussion is preserved as it was. New answers, comments, and votes are closed.