Script not erroring, not cloning the Gui either

Hey, reader!

I am currently working on a script that will clone a Billboard Gui in the nearest block in the folder in the workspace named “ATMS”.

The issue with the script is clear in the title, it’s not erroring, and also not doing it’s task.

My script:

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = game.Players.LocalPlayer
local Character = Player.Character

local ATMGui = ReplicatedStorage:WaitForChild("ATMGui")

local robTime = 5

local nearestATM, gui

local maxDistance = 15

local Robbed = false

Character.Humanoid.Died:Connect(function()
	if gui then
		gui:Destroy()
	end
	script.Disabled = true
end)

UserInputService.InputBegan:Connect(function(input, isProcessed)
	if not isProcessed then return end
	if not input.KeyCode == Enum.KeyCode.E then return end
	
	Robbed = true
end)

UserInputService.InputEnded:Connect(function(input, isProcessed)
	if not isProcessed then return end
	if not input.KeyCode == Enum.KeyCode.E then return end
	if not nearestATM then return end
	
end)

local toggleGui = function(toggle)
	if gui then
		gui:Destroy()
		gui = nil
	end
	
	if toggle == true then
		gui = ATMGui:Clone()
		gui.Parent = nearestATM.RobPart
	end
end

RunService.RenderStepped:Connect(function()
	local newNearestATM
	for _, ATM in pairs(workspace:WaitForChild("ATMS"):GetChildren()) do
		if (ATM.PrimaryPart.Position - Character.HumanoidRootPart.Position).magnitude <= maxDistance and not Robbed then
			maxDistance = (ATM.PrimaryPart.Position - Character.HumanoidRootPart.Position).magnitude
			newNearestATM = ATM
		end
	end
	
	nearestATM = newNearestATM
	
	if nearestATM and not Robbed then
		toggleGui(true)
	else
		toggleGui(false)
	end
end)

Could you please figure out what might be the issue?

Thanks in advance,
pranvexploder. :smiley:

P.S. I know that this is quite silly, and the cause of it is silly too, but I’m just not able to find the cause for this! Sorry for disturbing.

3 Likes

Hey,

I don’t see you declaring a gui variable anywhere.

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = game.Players.LocalPlayer
local Character = Player.Character

local ATMGui = ReplicatedStorage:WaitForChild("ATMGui")

local robTime = 5

local nearestATM, gui

local maxDistance = 15

local Robbed = false

local gui = nil

Character.Humanoid.Died:Connect(function()
	if gui then
		gui:Destroy()
	end
	script.Disabled = true
end)

UserInputService.InputBegan:Connect(function(input, isProcessed)
	if not isProcessed then return end
	if not input.KeyCode == Enum.KeyCode.E then return end
	
	Robbed = true
end)

UserInputService.InputEnded:Connect(function(input, isProcessed)
	if not isProcessed then return end
	if not input.KeyCode == Enum.KeyCode.E then return end
	if not nearestATM then return end
	
end)

local toggleGui = function(toggle)
	if gui then
		gui:Destroy()
		gui = nil
	end
	
	if toggle == true then
		gui = ATMGui:Clone()
		gui.Parent = nearestATM.RobPart
	end
end

RunService.RenderStepped:Connect(function()
	local newNearestATM
	for _, ATM in pairs(workspace:WaitForChild("ATMS"):GetChildren()) do
		if (ATM.PrimaryPart.Position - Character.HumanoidRootPart.Position).magnitude <= maxDistance and not Robbed then
			maxDistance = (ATM.PrimaryPart.Position - Character.HumanoidRootPart.Position).magnitude
			newNearestATM = ATM
		end
	end
	
	nearestATM = newNearestATM
	
	if nearestATM and not Robbed then
		toggleGui(true)
	else
		toggleGui(false)
	end
end)
1 Like

Well, the Gui is declared in the toggleGui() function. Declaring is not the problem. Thanks for the help! :smiley: