The Topic: I’m working on a city-RPG game that was inspired by an old game, with new mechanics as part of the role. One of the features includes a place indicator that tells the player where they are at, in different locations of the entire map.
The basic idea I had to achieve this was to consider using radius and magnitude and have at least three different locations that will tell the player their current general location.
I use a table to keep a list of the locations around the map as a starter.
For example, when a player enters LocationB from LocationA, a GUI will appear with the name of that location.
The Actual Issue:
--RAN FROM A LOCALSCRIPT
local InZone = false
local function CheckZone(Zone)
if (Zone[1] - LocalPlayer.Character.HumanoidRootPart.Position).magnitude <= Zone[2] then
if not InZone then
InZone = true
M.ZoneAppear(Zone[3],0.75,Zone[4]) -- passed parameters
elseif InZone then
InZone = false -- start of the issue
end
end
end
local CheckWait = coroutine.wrap(function()
while true do
R.Heartbeat:Wait() -- if the player was in LocationA:
CheckZone(Zones[1]) -- LocationA, returns true, fires the function
CheckZone(Zones[2]) -- LocationB, false
CheckZone(Zones[3]) -- LocationC, false
end
end)
CheckWait()
When CheckZone() runs with a chosen zone, it will check if the Player’s HumanoidRootPart is within the box. If it is within the box, InZone will be true, and a function from a ModuleScript will fire with passed parameters from that table. However, if it detects that it is NOT within the box, then it will set InZone to false.
However, the same function will fire with the other zones, and they will set InZone to false too.
At the speed of the server, CheckWait() will do this if the player is in LocationA:
- CheckZone(LocationA) will say it’s true and fire.
- CheckZone(LocationB) and (LocationC) will say it’s false and doesn’t fire.
- Repeat the same three functions.
End Result: The function from the ModuleScript will keep firing over and over until the Player is not within any location.
Solutions attempted:
Before using radius, I gave it a shot at using the .Touched events from an invisible non-collidable part in the same three locations.
- The parts think that the HumanoidRootPart has left and re-entered, firing the function again. Again, I attempted with using a local boolean value. Caused by using a VehicleSeat.
- SpawnLocations tend to set the player’s location wayyyyy up high first. It’s not noticed due to a delaying loading screen, but it is a problem still.
The actual thing I wanted:
The function from the ModuleScript to fire only once when a player enters a Location.
I didn’t see any other topics that involved the use of more than one radius, but if I’m wrong, I don’t mind being linked to it.
