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

How do I fix the problems with my debouncing?

-- [[ Variable Storage ]] --

local CommonTrigger = game.Workspace:FindFirstChild("CommonTrigger")

-- [[ Code ]] --

game.Players.PlayerAdded:connect(function(Player)
	local DebounceValue = Instance.new("BoolValue", Player)
	DebounceValue.Name = "DebounceValue"
	DebounceValue.Value = false
end)

function onTouched(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	if Player.DebounceValue.Value == false then
	Player.DebounceValue.Value = true
	Humanoid.WalkSpeed = 0
	local CommonUI = ReplicatedStorage:FindFirstChild("PT_UI").CommonUI:Clone()
	CommonUI.Parent = Player.PlayerGui
	CommonUI.Frame:TweenPosition(UDim2.new(0.5,-350,0.5,-200), "Out", "Elastic", 3)
	end
end

CommonTrigger.Touched:connect(onTouched)

The point of this script is to give every player a DebounceValue once they join the game. If they touch a certain brick, then a UI will slide down.

Now everything works fine and dandy EXCEPT my debounce method. If they touch the brick, it gives them like five of the same GUI, even with the debounce.

Before you ask why I did a BoolValue for Debounce, I honestly forgot why, but if you want to improve it or get rid of it, feel free.

Answers would be greatly appreciated!

1 answer

✓ Accepted answer

I would personally just use findFirstChild() to see if a Gui is already there.

-- [[ Variable Storage ]] --

local CommonTrigger = game.Workspace:FindFirstChild("CommonTrigger")

-- [[ Code ]] --

function onTouched(hit)
    local Humanoid = hit.Parent:FindFirstChild("Humanoid")
    local Player = Players:GetPlayerFromCharacter(hit.Parent)
    if not Player.PlayerGui:findFirstChild("CommonUI") then -- If the PlayerGui does not contain the CommonUI
    Humanoid.WalkSpeed = 0
    local CommonUI = ReplicatedStorage:FindFirstChild("PT_UI").CommonUI:Clone()
    CommonUI.Parent = Player.PlayerGui
    CommonUI.Frame:TweenPosition(UDim2.new(0.5,-350,0.5,-200), "Out", "Elastic", 3)
    end
end

CommonTrigger.Touched:connect(onTouched)

:FindFirstChild()

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