I’m attempting to create a jail system that locks a player into position, when using this method I notice a increase in performance usage and ping rising to above 400ms at times; when removing this from the script all of the issues are resolved. If anyone knows how to make this specifically use less memory that’d be great. The script is a ModuleScript ran by a LocalScript in StarterGui
spawn(function()
while true do
wait(.001)
for _, player in pairs(game.Players:GetPlayers()) do
if jailedPlayers[player.UserId] then
checkPlayerPosition(player)
end
end
end
end)
My mistake, my developer had moved it into a Server Script without my noticing; the code is managing a Command Bar for administrators. When a user is included in the ‘jailedPlayers’ table it ensures they remain in that table until removed. As seen here;
local function onPlayerAdded(player)
if jailedPlayers[player.UserId] then
local cellCFrame = jailedPlayers[player.UserId]
player.CharacterAdded:Connect(function(character)
local HumanoidRootPart = character:FindFirstChild('HumanoidRootPart')
if HumanoidRootPart then
HumanoidRootPart.CFrame = cellCFrame
end
end)
end
end
local function onCharacterAdded(character)
local HumanoidRootPart = character:FindFirstChild('HumanoidRootPart')
local player = game.Players:GetPlayerFromCharacter(character)
if HumanoidRootPart and jailedPlayers[player.UserId] then
HumanoidRootPart.CFrame = jailedPlayers[player.UserId]
end
end
local function checkPlayerPosition(player)
local character = player.Character
local HumanoidRootPart = character and character:FindFirstChild('HumanoidRootPart')
if HumanoidRootPart then
local cellCFrame = jailedPlayers[player.UserId]
if not cellCFrame or (HumanoidRootPart.Position - cellCFrame.Position).magnitude > 3 then
HumanoidRootPart.CFrame = cellCFrame
end
end
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(onCharacterAdded)
end)
spawn(function()
while true do
wait(.001)
for _, player in pairs(game.Players:GetPlayers()) do
if jailedPlayers[player.UserId] then
checkPlayerPosition(player)
end
end
end
end)
Well I’m going to assume that the script provided above runs on the server. You’re facing performance issues because you’re checking every single player even if they aren’t jailed every millisecond which isn’t the best approach.
Instead, I would personally loop over the jailed players table and see if the player is in the game. Then you can run your functions to check the position and stuff. However, I would add a wait to each iteration of player just to not stress the thread. Here is what that would look like:
(This was programmed on a phone, you will need to replace the quotation marks and indent this code)
— I’m going to assume the spawn function is apart of task.
task.spawn(function()
while true do
task.wait(0.5)
for userId, jailCFrame in pairs(jailedPlayers) do
local player = game.Players:GetPlayerByUserId(userId)
if player then
checkPlayerPosition(player)
task.wait(0.5)
end
end
end
end)
Again I apologize for the horrible formatting but this should give something for your developer to work with!
I’m primarily trying to make sure players remain within the jail regardless of current position or state, I decided I’ll use Character updates to define when to check a character instead of directly checking the table every millisecond. Thank you!