Local Script Runs Twice

Why is this local script running twice? Local script and Module are placed under “StarterPlayerScripts”

Local Script:

--- Module
local BorderManage = require(script.Parent:WaitForChild("BorderManage"))

--- Player
local Player = game.Players.LocalPlayer

--- Entrance Door
local Handle = workspace.Doors.EntranceDoor.Handle

--- UI
local PlayerGui = Player:WaitForChild("PlayerGui")
local ReturnButton = PlayerGui:WaitForChild("MainGui").Jobs.SmallMenu.Return

--- Function
print("T")
BorderManage.LoadInJobs()

Handle.ClickDetector.MouseClick:Connect(function() -- Collapses border and shows list of jobs
	BorderManage.CollapseBorder()
	wait(1)
	BorderManage.ShowJobList()
end)

ReturnButton.MouseButton1Click:Connect(function() -- returns border back to normal and does not show jobs anymore
	BorderManage.HideJobList()
	wait(1)
	BorderManage.DecollapseBorder()
end)

image

Module:

local Player = game.Players.LocalPlayer

local module = {}

function module.LoadInJobs()
	--- Service
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	
	--- UI to clone
	local JobList = ReplicatedStorage.ToCloneGui.JobList
	
	for i, v in JobList:GetChildren() do
		--print(i)
	end
end

function module.CollapseBorder()
	-- UI
	local MainGui = Player:WaitForChild("PlayerGui").MainGui
	local Upper = MainGui.Border.Upper
	local Bottom = MainGui.Border.Bottom
	
	Upper:TweenSize(UDim2.new(1.5,0,0.5,0), Enum.EasingDirection.In)
	Bottom:TweenSize(UDim2.new(1.5,0,-0.5,0), Enum.EasingDirection.In)
end

function module.DecollapseBorder()
	-- UI
	local MainGui = Player:WaitForChild("PlayerGui").MainGui
	local Upper = MainGui.Border.Upper
	local Bottom = MainGui.Border.Bottom

	Upper:TweenSize(UDim2.new(1.5,0,0.04,0), Enum.EasingDirection.Out)
	Bottom:TweenSize(UDim2.new(1.5,0,-0.04,0), Enum.EasingDirection.Out)
end

function module.ShowJobList()
	--- Service
	local TweenService = game:GetService("TweenService")
	
	--- UI
	local MainGui = Player:WaitForChild("PlayerGui").MainGui
	local BorderFancy = MainGui.Jobs.BorderFancy
	local Joblist = MainGui.Jobs.Joblist
	local SmallMenu = MainGui.Jobs.SmallMenu
	local FreeMouse = MainGui.FreeMouse
	
	FreeMouse.Modal = true
	
	BorderFancy.Visible = true
	
	BorderFancy.BackgroundTransparency = 0.95
	local BorderFancyTransparency = TweenService:Create(BorderFancy, TweenInfo.new(3), {ImageTransparency = 0})
	BorderFancyTransparency:Play()
	
	Joblist.Visible = true
	SmallMenu.Visible = true
end

function module.HideJobList()
	--- Service
	local TweenService = game:GetService("TweenService")

	--- UI
	local MainGui = Player:WaitForChild("PlayerGui").MainGui
	local BorderFancy = MainGui.Jobs.BorderFancy
	local Joblist = MainGui.Jobs.Joblist
	local SmallMenu = MainGui.Jobs.SmallMenu
	local FreeMouse = MainGui.FreeMouse
	
	FreeMouse.Modal = false
	
	Joblist.Visible = false
	SmallMenu.Visible = false

	local BorderFancyTransparency = TweenService:Create(BorderFancy, TweenInfo.new(1), {ImageTransparency = 1})
	BorderFancyTransparency:Play()
	BorderFancy.BackgroundTransparency = 1
	
	wait(1)
	
	BorderFancy.Visible = false
end

return module

Found out the script was running twice because it also runs in “StarterPlayerScripts” to prevent that I added this line on the very first top of the script.

if script.Parent.Name == "StarterPlayerScripts" then return end -- prevent the script from running while in "StarterPlayerScripts"
4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.