Im trying to have this function run one time once the event is fired, It works without the if detected ==false then
but once I add it, it just stops working as if to mock me and despite my best efforts of persuasion, it refuses to cooperate. Can anyone help me stop my code from misbehaving? Thank you.
local players = game:GetService("Players")
local funnyMen = collectionService:GetTagged("Enemy")
local detected = false
local function funnyFunc(funnyMan)
local nearestPlayer, nearestDistance
for _, player in pairs(players:GetPlayers()) do
local character = player.Character
local distance = player:DistanceFromCharacter(funnyMan.HumanoidRootPart.Position)
if not character or
distance > maxDistance or
(nearestDistance and distance >= nearestDistance)
then
continue
end
nearestDistance = distance
nearestPlayer = player
end
if nearestPlayer then
funnyMan.PrimaryPart.CFrame = CFrame.new(funnyMan.PrimaryPart.Position, Vector3.new(nearestPlayer.Character.PrimaryPart.Position.X, funnyMan.PrimaryPart.Position.Y, nearestPlayer.Character.PrimaryPart.Position.Z))
end
end
for i,v in pairs(funnyMen) do
local detectEvent = v:FindFirstChild("Detect")
detectEvent.OnServerEvent:Connect(function()
if detected == false then
funnyFunc(v)
detected = true
end
end)
end