I’m trying to achieve a better encounter system
in the next video the 1 printed when the touched event happend
in this video you can see that when forward and backwards, it doesn’t reacts the touched event, but when i jump it does.
that’s the script I’m using:
local function OnEncounter(hit)
if Players:FindFirstChild(hit.Parent.Name) then
print(1) -- the 1 in the console
local Player = Players:FindFirstChild(hit.Parent.Name)
if not Player:FindFirstChild("Debounce") then
local Debounce = Instance.new("BoolValue")
local point = math.random(1, encounterRate)
Debounce.Name = "Debounce"
Debounce.Parent = Player
if point == 1 then
-- enter battle
end
wait(2)
Debounce:Destroy()
end
end
end
field.Touched:Connect(OnEncounter)
but it just works really weird, anyone knows how can I improve it?
So if I’ll use regions, there are more than 1 fields, so i’ll have to go over all the fields and do something like this:
for _, field in pairs(fields) do
spawn(function()
-- make region with field part and then
while wait() do
parts = workspace:FindPartsInRegion3(region)
for i, part in pairs(parts) do
onEncounter(part)
end
end
)
end
like that?
with raycasting, i have no idea what to do with it lol.
and also wouldn’t regions be inefficient?