How to call only once this function

How to call this function only once?

local Guest = script.Parent;
local Humanoid = Guest:WaitForChild("Humanoid");

local Hotel = Guest.Parent.Parent;
local Next = Hotel:WaitForChild("Next");
local Queue = Hotel:WaitForChild("Queue");


Humanoid:MoveTo(Queue.Position)

local Deb = false;

local function Go_To_Room()
	print("Going to my room :)")
	Guest:Destroy();
end;

Next:GetPropertyChangedSignal("Value"):Connect(function()
	if not Deb then
		Deb = true;
		Go_To_Room();
	end
end);

Output:

  19:28:54.919  Going to my room :)  -  Server
  19:28:54.920  Going to my room :)  -  Server

Try this maybe i dont know if it would help

Workspace.Hotel_1.Guests.Guest.Functions:22: attempt to index nil with ‘Disconnect’

conn:Disconnect() is underlined red

You can use Once instead of Connect.

Next:GetPropertyChangedSignal("Value"):Once(Go_To_Room);
1 Like

It shouldn’t be printing twice, so I’m assuming the script was cloned somehow? You should print(script) instead of the other thing, to see if that’s the case. Click the Script in the output to see where both are.

Not working :frowning:
Still same result.

Print the script variable to see if it’s being cloned.

19:44:58.513 Functions - Server
19:44:58.514 Functions - Server
Same location

Then i assume its not in the script side as @bluebxrrybot stated

Okay well I think you should use breakpoints to see what the script is doing.

I will post both my scripts here, one is changing Next.Value with proximity function.

First:

local Hotel = script.Parent;
local Guests_Folder = Hotel:WaitForChild("Guests");
local Guests_Spawn = Hotel:WaitForChild("Guests_Spawn");
--;
local Owner = Hotel:WaitForChild("Owner");
local Next = Hotel:WaitForChild("Next");
--;
local Check_In = Hotel:WaitForChild("Check_In");
local Check_In_Proxi = Check_In:WaitForChild("ProximityPrompt");

--// CREATE GUEST
local function Create_Guest()
	local Guest_Clone = Guest:Clone();
	Guest_Clone.Parent = Guests_Folder;
	Guest_Clone:MoveTo(Guests_Spawn.Position);
	Guest_Clone:FindFirstChild("Functions").Enabled = true;
end;

--// WHILE WAIT
repeat wait(.1) until Owner.Value

if (Owner.Value) then
	print("Creating Guest")
	Create_Guest()
end;

Check_In_Proxi.Triggered:Connect(function()
	print("Checked In")
	local Guest_Clone = Guest:Clone();
	Guest_Clone.Parent = Guests_Folder;
	Guest_Clone:MoveTo(Guests_Spawn.Position);
	Guest_Clone:FindFirstChild("Functions").Enabled = true;
	Next.Value = Next.Value + 1;
end);

Second inside guest:

local Guest = script.Parent;
local Humanoid = Guest:WaitForChild("Humanoid");

local Hotel = Guest.Parent.Parent;
local Next = Hotel:WaitForChild("Next");
local Queue = Hotel:WaitForChild("Queue");


Humanoid:MoveTo(Queue.Position)


local function Go_To_Room()
	print(script)
end;

Next:GetPropertyChangedSignal("Value"):Once(Go_To_Room);

Output after proximity triggered:

  19:51:50.072  Creating Guest  -  Server - Guest_Control:32
  19:51:54.978  Checked In  -  Server - Guest_Control:37
  19:51:54.981  Functions  -  Server - Functions:13
  19:51:54.981  Functions  -  Server - Functions:13

Explorer screenshots:
Screenshot 2023-01-26 195523

Screenshot 2023-01-26 195617

In the first script, in

if (Owner.Value) then
	print("Creating Guest")
	Create_Guest()
end;

Create_Guest() creates a clone of Gest and executes the Functions script where a first connection to Next:GetPropertyChangedSignal(“Value”) is created.
And when Check_In_Proxi is triggered a second connection to Next:GetPropertyChangedSignal(“Value”) is created.

Check_In_Proxi.Triggered:Connect(function()
	print("Checked In")
	local Guest_Clone = Guest:Clone();
	Guest_Clone.Parent = Guests_Folder;
	Guest_Clone:MoveTo(Guests_Spawn.Position);
	Guest_Clone:FindFirstChild("Functions").Enabled = true;  <----
	Next.Value = Next.Value + 1;
end);

So when you update the value of Next,

Next.Value = Next.Value + 1;

it fires both connected events, so it prints twice.

A simple solution is to update the value of Next before creating the second connection (i.e. before enabling the new clone script).

Check_In_Proxi.Triggered:Connect(function()
	print("Checked In")
	local Guest_Clone = Guest:Clone();
	Guest_Clone.Parent = Guests_Folder;
	Guest_Clone:MoveTo(Guests_Spawn.Position);
	Next.Value = Next.Value + 1;
	Guest_Clone:FindFirstChild("Functions").Enabled = true;
end);
1 Like

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