Event Firing Twice

The Reached event fires twice for some reason, I tried adding debounce already. I just want it to fire once

This event is from SimplePath: SimplePath - Pathfinding Module

local Rs = game:GetService("ReplicatedStorage")
local NoiseEvent = Rs:WaitForChild("Events"):WaitForChild("PlayerNoiseEvent")
local SimplePath = require(Rs.SimplePath)

local Dummy = workspace.BlindMonster3
local state = "patrol"

local waypoints = workspace:WaitForChild("Waypoints"):GetChildren()

local Path = SimplePath.new(Dummy)
Path.Visualize = true

local Goal = Dummy.PrimaryPart.Position 

NoiseEvent.OnServerEvent:Connect(function(plr, sound, pos)
	print(plr, sound, pos)
	Goal = pos
	state = "chase"
	print(state)
end)

local function patrol()
	if Path.Status == "Active" then return end
	if state == "chase" then return end
	print("patrolling")
	local ranPoint = waypoints[math.random(1, #waypoints)]
	Goal = ranPoint.Position
end

Path.Reached:Connect(function()
	if state == "patrol" then
		patrol()
		print("test")
	elseif state == "chase" then
		state = "patrol"
	end
end)

while true do
	Path:Run(Goal)
end

So basically if its state is chase it will switch to patrol when path is reached, for some reason it fires twice when switching to patrol mode

It firing twice is likely to do with the SimplePath module instead of your script, but i think that wont affect the gameplay.

if u dont want any prints duplicating twice here’s a safeprint function that doesnt allow any duplicate prints :

local lastprint = nil

local function safeprint(string)
	
	if tostring(lastprint) == tostring(string) then
	 -- do whatev u want if its duplicated...
	elseif tostring(lastprint) ~= tostring(string) then
		print(string)
		
		lastprint = string
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.