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

Why does my script only run twice, but no more times???

Asked by
Faazo 84
6 years ago

I have this script that opens a ScreenGui when the detector is touched. It works perfectly the first two times, but the thing is that after two times of opening and closing it doesn't work anymore. Here is my script

    Players = game:GetService("Players")
    LocalPlayer = Players.LocalPlayer
    PlayerGui = LocalPlayer.PlayerGui
    Detector = game.Workspace.RoomDetector1
    RS = game:GetService("ReplicatedStorage")

ShopOpen = false

Detector.Touched:Connect(function(touch)    
    if ShopOpen == false and touch.Parent.Name==LocalPlayer.Name then
    RS.Rooms:Clone().Parent = PlayerGui  
    ShopOpen=true
    end
end)

game.Players.LocalPlayer.PlayerGui:WaitForChild("Rooms").Frame.Close.MouseButton1Click:Connect(function()
    ShopOpen = false   
    print ("Test")
end)

I also noticed that it only prints test once. This is really weird. Help is appreciated.

0
You don't have a loop in here even. chasedig1 115 — 6y
0
ohhhhh but how come it runs a second time without a loop? Faazo 84 — 6y
0
Chase, do you know how catastrophic a touched event would be if you ran it on a loop? Imagine this, right. You have one touched function, and you put it inside a loop. Now, every 0.04 seconds, it multiplies the touched function. Now, when you touch the part that you gave the event to, it runs a billion times in one touch. Would you really want that? Fifkee 2017 — 6y
0
Add a debounce Earthkingiv 51 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

The best solution for this is to have two different scripts located in different spots. The first script which is Gui cloning script is going to be placed in StarterPlayerScript whereas the other script which is Removing Gui script is going to be placed inside the Button itself.

Gui Cloning Script

-- Declaration Section 
--//Game Services 
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game:GetService("Players").LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui")

--//Workspace Assets
local Part = Workspace:FindFirstChild("Detector")

--//ReplicatedStorage Assets
local ScreenGui = ReplicatedStorage:FindFirstChild("ScreenGui")
local Button = ScreenGui.Frame:FindFirstChild("TextButton")

--//Variables 
local NewGui
local debounce = false 

-- Processing Section 

local function openGui (hit)
    if hit.Parent.Name == game.Players.LocalPlayer.Name and PlayerGui:FindFirstChild("ScreenGui") == nil then
        if debounce then return end 
        debounce = true 
        NewGui = ScreenGui:Clone()
        NewGui.Parent = PlayerGui

        debounce = false 
        print("Done")
    end 
end

Part.Touched:Connect(openGui)

Removing Gui Script

-- Declaration Section 
local button = script.Parent 

-- Processing Section 
button.MouseButton1Click:Connect(function()
    game.Players.LocalPlayer:WaitForChild("PlayerGui").ScreenGui.Enabled = false 
    game.Players.LocalPlayer:WaitForChild("PlayerGui").ScreenGui:Destroy()
end)

Have a lovely day of coding.

Ad

Answer this question