local ReplicatedStorage = game:GetService("ReplicatedStorage") local humanoid =…
- User
local ReplicatedStorage = game:GetService("ReplicatedStorage") local humanoid = script.Parent local npcModel = humanoid.Parent local returnPoint = game.Workspace:WaitForChild("powrotmng") local PlayerEvent = ReplicatedStorage:WaitForChild("Player") local eventConnected = false while true do script.Parent.Script.Enabled = false
task.wait() if not returnPoint then warn("Return point not found!") break end humanoid:MoveTo(returnPoint.Position) humanoid.MoveToFinished:Wait() print("przyszedl") PlayerEvent.OnServerEvent:Connect(function(player) print("no ten 1") PlayerEvent:FireClient(player) print("Signal sent to client") end) eventConnected = trueend
it need to send that fireclient 1 time only and the same with prints here
Assistant
You're on the right track with your script, but there are a few key issues that need to be addressed:
Issues in your current code:
-
Infinite Loop Misuse:
- You're running an infinite loop (
while true do), which continuously disablesScript, moves the NPC to a position, and connects a new event handler every time. - This causes multiple connections of the same event (
PlayerEvent.OnServerEvent:Connect(...)), leading to multiple prints and multiple signals being fired.
- You're running an infinite loop (
-
Connecting Events Inside a Loop:
- Every loop iteration creates a new connection to
PlayerEvent.OnServerEvent, causing multiple triggers when only one is desired.
- Every loop iteration creates a new connection to
-
Unnecessary Disabling of Script:
script.Parent.Script.Enabled = falseseems redundant unless you have another LocalScript or Script named "Script" that you're trying to disable.
Goal Clarification:
From what I understand, you want:
- The NPC to move to a specific point once.
- Once it arrives, connect the
PlayerEventonce, and fireFireClient(player)only once per player interaction. - Print debug messages only once.
✅ Corrected Version of Your Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local humanoid = script.Parent local npcModel = humanoid.Parent local returnPoint = workspace:WaitForChild("powrotmng") -- References to the event local PlayerEvent = ReplicatedStorage:WaitForChild("Player") -- Make sure we don't connect the event multiple times local eventConnected = false -- Disable any unnecessary scripts if needed script.Parent.Script.Enabled = false -- Move NPC to the return point humanoid:MoveTo(returnPoint.Position) humanoid.MoveToFinished:Wait() print("przyszedl") -- Should print only once -- Connect the event only once if not eventConnected then PlayerEvent.OnServerEvent:Connect(function(player) print("no ten 1") PlayerEvent:FireClient(player) print("Signal sent to client") end) eventConnected = true end
✅ If You Want It to Trigger Only Once Per Player (Optional Enhancement):
If you want to ensure the event fires only once per player, you can use a table to track who's already been processed:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local humanoid = script.Parent local npcModel = humanoid.Parent local returnPoint = workspace:WaitForChild("powrotmng") local PlayerEvent = ReplicatedStorage:WaitForChild("Player") local hasFired = {} script.Parent.Script.Enabled = false humanoid:MoveTo(returnPoint.Position) humanoid.MoveToFinished:Wait() print("przyszedl") -- Connect event once PlayerEvent.OnServerEvent:Connect(function(player) if not hasFired[player] then print("no ten 1") PlayerEvent:FireClient(player, "someData") -- Optional arguments print("Signal sent to client") hasFired[player] = true end end)
Summary:
- Avoid placing event connections inside loops.
- Use flags like
eventConnectedorhasFiredto ensure actions happen only once. - Ensure you're not disabling unrelated scripts unnecessarily.
Let me know if you also want this behavior for each new round or respawn!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.