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

How to prevent button from duplicating Trail if clicked more than once?

This button in my GUI keeps duplicating the Trail after it's clicked more than once. The button is suppose to let the player get one Trail but it keeps duplicating the trail if the player clicks more than once.

local player = game.Players.LocalPlayer
local give = script.Parent

give.MouseButton1Click:connect(function()
	local character = player.Character
	local trailClone = Instance.new("Trail", character.Head)
	local HP = character.Head.NeckRigAttachment
				local HRP = character.HumanoidRootPart.RootAttachment
				local trailClone = Instance.new("Trail", character.Head)
				trailClone.Parent = character.HumanoidRootPart
				trailClone.Attachment0 = HP
				trailClone.Attachment1 = HRP
				trailClone.Lifetime = 1
				trailClone.Texture =  "http://www.roblox.com/asset/?id=1311255790" 
				trailClone.TextureMode = "Stretch"
				trailClone.TextureLength = 1
				trailClone.LightEmission = 0.2
				trailClone.Transparency = 0.5
				trailClone.MinLength = 0.1


end)

Destroy script:

local player = game.Players.LocalPlayer
local give = script.Parent

give.MouseButton1Click:connect(function()
    local char = player.Character
    for _, v in pairs(char.HumanoidRootPart:GetChildren()) do if v.Name == "Trail" then v.Parent = nil end end
   
end)

1 answer

Booleans are an easy debounce

local trail = false

give.MouseButton1Click:connect(function()
	if trail then return end
	trail = true
end)

Add your script into that thing and it should be perfect

Edit: actually since your player may die or something you may want to just check if the trail currently exists on the player, you can do that through FindFirstChild

give.MouseButton1Click:connect(function()
	local character = player.Character
	if character.Head:FindFirstChild("Trail") then return end
end)

1 comment

0

I added my script into your code and it works but it won't let me create another Trail after it's destroyed the first time.

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