Server Locking Script

val = game.ReplicatedStorage.tlock



game.ReplicatedStorage.slock.OnServerEvent:Connect(function(plr)
	
	val.Value = false
	
	
	while true do
		if val.Value == true then
				break
				end	
			
		game.Players.PlayerAdded:Connect(function(plr)
			plr:Kick("Server Locked")
			wait()
			
			end)
				wait()
				
		wait()		
	end
	
		wait()
end)



Even after the value is equal to true, the player still gets kicked. I’ve tried debugging with prints but that was no use. Any help is appreciated. Thank you :slight_smile:

1 Like

Your code looks like it should work, how are you setting the value?

1 Like

@tehgreatdoge
The value is being set by a TextButton,
image
UnSlock makes the value true, and Slock makes it false. However when clicking UnSlock, it still decides it still wants to kick people lol.

That would be your problem, changes to objects under replicated storage do not replicate to the server.

1 Like

This in general is really bad practice, putting events under while loops. And creating new while loop each time an event fires. This will create a big mess. This can be simplified soo much.
Also you don’t need to store the value in a value object, just have a variable in the script indicating wether you lock or not. And also, don’t make two remote events one for each button to change the value, just make one, and each time a button is clicked fire the same remote passing wether true or flase.

local lock = false

game.ReplicatedStorage.slock.OnServerEvent:Connect(function(plr, val)
     lock = val
end)

game.Players.PlayerAdded:Connect(function(plr)
     if lock then
          plr:Kick("Server Locked")
     end
end)

This might help as well.

3 Likes

Ah, so I should put the value in workspace?

The PlayerAdded event will still run since you never disconnected it. Try something like this:

val = game.ReplicatedStorage.tlock

local connection

local function playerAdded(plr)
	plr:Kick("Server Locked")
	wait()

end


game.ReplicatedStorage.slock.OnServerEvent:Connect(function(plr)
	
	val.Value = false
	
	
	while true do
		if val.Value == true then
			if connection then
				connection:Disconnect()
			end
			break
		end	
			
		connection = game.Players.PlayerAdded:Connect(playerAdded)
				wait()
				
		wait()		
	end
	
		wait()
end)
2 Likes

No, you should use a remote event to toggle it.

val.Value = not val.Value

while val.Value==true do

and do as thetacah said as well

1 Like

I would suggest simply modifying what you connect to the PlayerAdded event. Instead of if val.Value is true (which isn’t what you’re looking for based on your remote event code), check if the server is locked or not by if val.Value == false then instead of if val.Value. @starmaq’s solution is what you should do, as it is checking whether or not it should kick, instead of if the value actually exists.

3 Likes

Checking if val.Value checks if it’s truthy. That means the condition will go through if val.Value is anything but false or nil; it’s not checking if it exists.

7 Likes

Updated appropriately. Thanks for the comment, not sure how I forgot.

2 Likes