Why does this script only fire for 1 person in a 2 player server?

I’m currently trying to figure out why this won’t work for 2 players probably a stupid mistake, or how im running it.

Basically what im doing is making a loop that runs once with wwd, (while wait() do) and then having it send a remote to the server and break.

After the server recieves it it gives them a plot which works fine, but the local player script only works for one person.

The Script that I tried:

while wait() do
	if game.Players.LocalPlayer.OwnsAPlot.Value == false then
		game.ReplicatedStorage.Events.giveplot:FireServer()
		print("fired giveplot")
		wait(100)
		break
	end
end
2 Likes

Note: The wait 100 is a debounce ish thing

1 Like

You should change it to this:

db = false
while (game.Players.LocalPlayer.OwnsAPlot.Value == false and db == false) do
    db = true
    game.ReplicatedStorage.Events.giveplot:FireServer()
    print("fired giveplot")
    wait(certain time)
    db = false
end

the wait 100 is not a debounce system, and what im showing is a proper one.

2 Likes

Break stops your loop so it doesn’t runs again instead use

1 Like

I wasn’t using the wait as a full debounce system and I only want it to run once

1 Like
  • Why would you need to debounce?
  • For all its faults, Roblox has a nice Events system–Why not use it for this?
1 Like

The debounce was to prevent it from running before it broke originally but then I found out the own the plot value can do the same

1 Like

Use the Changed event instead of a while loop, it’s more efficient:

local player = game.Players.LocalPlayer
local debounce = true

player.OwnsAPlot.Changed:connect(function(value)
    if debounce then
        debounce = false
        game.ReplicatedStorage.Events.giveplot:FireServer()
        print("Fired")
        wait() -- I recommend *not* using a wait() function here, use a BindableEvent or something to tell the script when the event is complete
        debounce = true
    end
end)
1 Like

I mean the error wasn’t in the debounce or the wait it was that on the live server it was only firing for one person

1 Like

If you’re using the print() function from the script for feedback, it’s only going to print on your client. Make sure it’s printing on the server.

1 Like

Yours is excessive. Using a variable only matters if the code has a chance to run multiple times before the first iteration is finished. Since this is a loop and not a function, a wait will suffice, but I wouldn’t call it a debounce. Regardless, a debounce just isn’t necessary in a loop.

2 Likes

Break completely stops the loop so it would only once and only for one person you should instead use

for x,y in pairs (game.Players:GetPlayers())do
--code Y is the player and X is a Number
end
2 Likes

No, because this is on the client and will therefore run for all players.

2 Likes

A more optimized way of doing is:

local Connection

Connection = game.Players.LocalPlayer.OwnsAPlot:GetPropertyChangedSignal("Value"):Connect(function()
	if game.Players.LocalPlayer.OwnsAPlot.Value == false then
		game.ReplicatedStorage.Events.giveplot:FireServer()
		Connection:Disconnect()
	end
end)

Only detects if Value property changes, if false then fire event and Disconnect.

1 Like

Because the local scripts are local! They will work for one person as a individual only!

And what I was thinking with my local script was it fired once for every player who joins

That is what will happen.

30 characters

Local scripts fired once for everyone who joined because you used break which stoped the loop a complete solution is debounce so you should use

db = false
while wait() do
if game.Players.LocalPlayer.OwnsAPlot.Value == false and db == false then
    db = true
    game.ReplicatedStorage.Events.giveplot:FireServer()
    print("fired giveplot")
    wait(2)
    db = false
end
end

Actually I think CALIBRE’S response may work

I’ll try yours and his Thanks!