Bank System using Region3

Hey! Over the past day I’ve worked on some sort of bank system and would like some feedback on its efficiency and anything else that comes to mind.

Right now, this only checks if they’re in or out of the bank. I’m still working on giving cash and a timer but I’d like to get a review before committing fully.

The main bit is the function Robbery(part), you can ignore everything else if you wish to. Just wanted to include everything.

Full code:

local Players = game:GetService('Players')
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Events = ReplicatedStorage.Events
local BankEvent = Events.BankEvent

local Settings = script.Settings

local Open = Settings.Open
local Robbing = Settings.Robbing
local Rob_Amount = Settings["Rob Amount"]
local Vault = Settings.Vault.Value

--local vaultPart = workspace.Map.Bank.Vault
local maxParts = 20
local ignore = {}

local PlayersTag = {}

local chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-={}|[]`~'

Players.PlayerAdded:Connect(function(plr)
	local length = 12
	local randomString = ''

	local charTable = {}
	for c in chars:gmatch"." do
		table.insert(charTable, c)
	end

	for i = 1, length do
		randomString = randomString .. charTable[math.random(1, #charTable)]
	end
	
	local boolValue = Instance.new('BoolValue', plr)
	boolValue.Name = randomString
	
	PlayersTag[plr] = {}
	table.insert(PlayersTag[plr], {
		string1 = boolValue
	})
end)

function RobberyTime()
	local OpenTime = math.random(5, 10)
	wait(OpenTime)
		
	print('Robbing may begin')
	Open.Value = true
	
	wait(.5)	
	
	Robbery(Vault)
end

Open.Changed:Connect(function()
	if Open.Value == false then
		RobberyTime()
	end
end)


function CreateRegion3FromPart(Part)
    return Region3.new(Part.Position - (Part.Size/2), Part.Position + (Part.Size/2))
end

function Robbery(part)
	local Robbers = {}
	while wait(.5) do
		if Open.Value == true then
			local region = CreateRegion3FromPart(part)
			local partsInRegion = workspace:FindPartsInRegion3(region, nil, math.huge)
			
			for _, Player in next, Players:GetPlayers() do
				local check = false
				for _, v in next, partsInRegion do
					if Players:GetPlayerFromCharacter(v.Parent) then
						check = true
						local Character = Player.Character
							
						if Character and Character.Humanoid.Health > 0 then
							local myTag = PlayersTag[Player][1].string1
							
							myTag.Value = true
							table.insert(Robbers, Player)
							print(Player.Name.." has begun robbing")
						end
							
					else
						-- it's a part
							
					end
				end
				
				wait()
				
				if check == false then
					local myTag = PlayersTag[Player][1].string1
					myTag.Value = false
					
					for i, plr in next, Robbers do
						if plr == Player then
							table.remove(Robbers, i)
							print(Player.Name.. " has left the vault")
						end
					end
					
				end
			end
		else
			break
		end
	end	
end

RobberyTime()

Thanks!

5 Likes

Seems a bit inefficient don’t you think? Doesn’t seem wrong, but I feel like there could be more effective ways to go about this.

I was looking on the Lua website for making random strings, not too sure if there’s any other way of getting all the characters for it though.

There are other ways of getting random strings but the easiest method is to have a string of all the characters and to mash them together.

Alternatively you could use an external api or something of that nature but it is way easier to do it this way.

3 Likes

string.char()? You can use string.char(math.random(33,126)) to get random characters.
Here’s a list of character that you’ll get:

image

To your main problem, maybe use the .touched event? I know it isn’t the best method but it doesn’t require a loop. If you don’t want to use .touched maybe just compare the distance between the bank and the player.

5 Likes