Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't my script work twice in the same game?

Asked by 4 years ago

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.

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 AntoninFearless 622 — 4y
0
Check out my answer! WideSteal321 773 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

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.

0
Danke turquoise_monkeyman 32 — 4y
Ad

Answer this question