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 my script work twice in the same game?

I made a script to open guis but when I run the script for the second time in the game then the guis won't open. How do I fix it? this is my script:

part = script.parent.clicker
part.MouseClick:Connect(function(player)
local char = player.Character
local hum = char:WaitForChild("Humanoid")
print("Opening the Gui")
print(part)
local PlayerGui = player.PlayerGui

local GuiToBeOpened = PlayerGui:WaitForChild("ssdssdsds")
local clone = game.ServerStorage.LocalScript:Clone()
clone.Parent = GuiToBeOpened["TextButton"..value]
GuiToBeOpened.TextBox.Visible = true
GuiToBeOpened.TextButton1.Visible = true
GuiToBeOpened.TextButton2.Visible = true
GuiToBeOpened.TextButton3.Visible = true
GuiToBeOpened.TextButton4.Visible = true
end
end)

When I run the script the second time the words I print are in the output area but the gui wont open. I would be grateful if you could help.

2 comments

0

try adding a debounce? in the beginning add a local debounce = false and under the function do: If debounce == false then debounce = true and then your code in the bottom, after your done with the code add a wait(1) debounce = false maybe that will work

0

Check out my answer!

1 answer

✓ Accepted answer

How do I fix my issue?

Use Debounce, this would most probably fix your issue.

What is a debounce?

Debounce is a system of code that prevents something from happening too many times.

How do I use debounce?

It's very simple, just create a variable, let's call it debounce, and set it to false like this:

local debounce = false

Then, right after your MouseClick function, you would need to check if it still set to false, set it to true, and then right after your code is complete set it to false again:

part.MouseClick:Connect(function(player)
if not debounce then -- If debounce is false
debouce = true -- Set debounce to true
-- Do stuff
debounce = false -- set debounce to false
end
end)

Complete code:

local debounce = false
part = script.parent.clicker

part.MouseClick:Connect(function(player)
if not debounce then
debounce = true
local char = player.Character
local hum = char:WaitForChild("Humanoid")
print("Opening the Gui")
print(part)
local PlayerGui = player.PlayerGui

local GuiToBeOpened = PlayerGui:WaitForChild("ssdssdsds")
local clone = game.ServerStorage.LocalScript:Clone()
clone.Parent = GuiToBeOpened["TextButton"..value]
GuiToBeOpened.TextBox.Visible = true
GuiToBeOpened.TextButton1.Visible = true
GuiToBeOpened.TextButton2.Visible = true
GuiToBeOpened.TextButton3.Visible = true
GuiToBeOpened.TextButton4.Visible = true
debounce = false
end
end
end)

But that still didn't explain it to me well enough!

Well I can't give much of a better explanation than that, so check out this link, it goes much more in-depth than me.

1 comment

0

Danke

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