Hello! I am trying to make a bank robbery system for my game, but the problem is it just stopped working. There aren’t any errors in the output either. It was working before, and I don’t know what I changed to make it stop. It looks good to me.
Here is the code for the start of the bank robbery:
while bank:FindFirstChild("IsOpen").Value == true do
bankText.TextColor3 = OPEN_COLOR
bankDoor.CanTouch = true
bankDoor.Touched:Connect(function(hit)
print("hit")
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
isIP = true --in progress bool value
bankDoor.CanCollide = false
bankDoor.Transparency = 1
bankDoor.CanTouch = false
player.PlayerGui.RobberyGUI.AlertText.Visible = true
alert.Value = player.Name.." has started a bank robbery!"
if alreadyCloned == false then --clone money bags into the vault
alreadyCloned = true
for i, v in pairs(BMB) do
local bagClone = v:Clone()
bagClone.Parent = game.Workspace
end
end
wait(5)
player.PlayerGui.RobberyGUI.AlertText.Visible = false
if dyn:FindFirstChild("ProximityPrompt") then --dynamite prompt (to escape after robbing)
dyn.ProximityPrompt.Enabled = true
end --if the vaultdoor was opened beforehand
if vaultDoor.Position ~= Vector3.new(302.75, 7.5, 639.5) then
vaultDoor.Position = Vector3.new(302.75, 7.5, 639.5)
vaultDoor.Orientation = Vector3.new(0, -90, -90)
end
if vaultDoor:FindFirstChild("ProximityPrompt") then --vault prompt (to open vault)
vaultDoor.ProximityPrompt.Enabled = true
end
end
end)
local isEnded = false
if isIP == true and dwall.Transparency == 1 then --if in progress and the dynamite wall has been exploded
if isEnded == false then
isEnded = true
bank:FindFirstChild("IsOpen").Value = false
bank:FindFirstChild("IsInProgress").Value = false
wait(5.5)
CloseBank()
end
end
end
wait(.5)
end
The problem is that I can’t touch the door to start the robbery. Whenever I move my character to touch the door, the event just doesn’t run. I’ve checked that CanTouch is enabled, but it’s not working.
Could someone please help me out? I have no idea what’s wrong, it used to work. Thank you.
The loop is checking if the isOpen value is true, if not it stops and wont run anymore, even if you change the value
the next thing is that you are connecting functions every time the loop repeats, which will stack everything
you should instead:
– detect when the value is changed, if its true then add the event that detects door touch
after the bank is closed or the doors are open you are supposted to disconnect this event, you can use :Once in order to make the doors event be automatically disconnected after the doors are touched
example:
local doorConnection
bank.IsOpen.Changed:Connect(function()
if bank.IsOpen.Value then
doorConnection = bankDoor.Touched:Once(function(h)
local player = players:GetPlayerFromCharacter(h.Parent)
if player then
end
end)
else
if doorConnection then
doorConnection:Disconnect()
end
end
end)
Thanks so much! Inside the changed function, could I also detect when the value is false? That way I could run the closing code as well? (It resets the vault, cash, etc)
Something like:
elseif bank.IsOpen.Value == false then
—run closing code
—run the door disconnect event
I’m not the most experienced scripter, but thanks for your help!
(I’m also writing this on my phone, so sorry if my responses are delayed)
--[[
bank.IsOpen -- your instance which is bool value holder
bank.IsOpen.Changed -- the event of your instance
:Connect - the event method that connects event to the function
function -- the function that fires when event is fired
Changed event is fired everytime the value is changed so if you do
it works like:
event.Changed:Connect(function)
--]]
local funciton onOpenChanged()
if bank.IsOpen.Value then
-- the value is true
else
-- the value is false
end
end
bank.IsOpen.Changed:Connect(onOpenChanged)
Tip:
you don’t need to do
if bank.IsOpen.Value == true then
because it holds only 2 types of value, false and true
if statement checks if the arguments are true so if the value is true it fires the code under if statement, if not then it checks for elseif’s , and then for else
so like:
if true then
else
-- its anything else than true, but it can be only true or false so its false
end