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
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)
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)
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.
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.