Hello, quick question. So today I am trying to make robbable ATMS in one code. Basically one code will be handling all ATMs around the map. I have a module that handles all the existing ATMs placed around the map. I put two for now. Let’s get to the problem!
So the problem is the script works fine. I have a client script that will require the module. But somehow I did a for loop through all the atms and require the module, pass the arguments to it and this happened:
As you can see, An E Icon shows up to one of them but when I walk to the other one, nothing happens… Why? I’ll be showing the code.
Module script:
local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local RunService = game:GetService("RunService")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart") or Character.PrimaryPart
local DefaultBarSize = UDim2.new(0,10,0,10)
local FullBarSize = UDim2.new(0,100,0,10)
local PlayerGui = Player:WaitForChild("PlayerGui")
local StarterGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local LabelButton = PlayerGui.MiniHeist.RobATM.ImageLabel
local ATMHeist = {}
function ATMHeist:RegisterATM(ATMModel)
local StatusFolder = ATMModel.Status
UserInputService.InputBegan:Connect(function(keyCode)
if keyCode.KeyCode == Enum.KeyCode.E then
local Magnitude = (ATMModel.RobATM.Position - HumanoidRootPart.Position).Magnitude
if Magnitude <= 6 and StatusFolder.Open.Value == true then
LabelButton.Loader.Visible = true
while UserInputService:IsKeyDown(Enum.KeyCode.E) do
wait()
LabelButton.Loader.Fill.Size = LabelButton.Loader.Fill.Size + UDim2.new(0,1,0,0)
if LabelButton.Loader.Fill.Size == FullBarSize then
PlayerGui.UISounds.Cash:Play()
LabelButton.Loader.Fill.Size = DefaultBarSize
LabelButton.Loader.Visible = false
LabelButton.Visible = false
ATMModel.RobATM.RobATM.Event:FireServer({reason = "RobATM"})
--script.Disabled = true
break
end
if not UserInputService:IsKeyDown(Enum.KeyCode.E) then
--print("E key is not held... ")
LabelButton.Loader.Fill.Size = DefaultBarSize
wait(0.1)
LabelButton.Loader.Visible = false
break
end
end
end
end
end)
RunService.RenderStepped:Connect(function()
LabelButton.Visible = false
local Magnitude = (ATMModel.RobATM.Position - HumanoidRootPart.Position).Magnitude
if Magnitude <= 6 and StatusFolder.Open.Value == true then
LabelButton.Visible = true
local ScreenPoint = workspace.CurrentCamera:WorldToScreenPoint(ATMModel.RobATM.CFrame.Position)
LabelButton.Position = UDim2.new(0,ScreenPoint.X,0,ScreenPoint.Y,0)
end
end)
end
return ATMHeist
Localscript requiring the module:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ATMModule = require(ReplicatedStorage.Modules.ATMHeist)
local ATMModels = workspace.ATMs:GetChildren()
for i,v in pairs(ATMModels) do
if v:IsA("Model") then
ATMModule:RegisterATM(v)
end
end
----More Details----
How does the module work?
Answer: The module works as a handler for all the ATM models scattered around the map. So there are two ATMs in the map. It’s supposed to handle both of them. When required, there will be a function named “RegisterATM”. When called, it contains one parameter called “ATMModel” that means the model of every ATM on the map. It will register the model to the module as a parameter.
How is it required?
Answer: Before requiring the module, create 3 variables first. ReplicatedStorage service, the module, get all children of the ATMs folder in workspace (meaning it will get the two atms on the map). Then make a for in pairs loop then check if its a model then require the module, Call the function named “RegisterATM” then put “v” in the parameters. “v” means the variable of all the ATM models.
If you have any suggestions, reply below.
Got anymore questions to ask me? Ask below in the replies.
All help is appreciated!
Thanks in advance!