Script Frozens The Game in The player

When these lines Of code executes, It Just Frozen The game, in Studio It don’t happen, only in Roblox player

Script 1:

        for i, player in game:GetService("Players"):GetPlayers() do
            if player.Play.Value == true then
                if player.Character then
                    game.ReplicatedStorage.Events.ChangePlayerCharacter:Fire(player)
                    player.PlayerGui.ItemInventory.Enabled = false
                    player.PlayerGui.Codes.Enabled = false
                end
            end
        end

Script 2:

local function respawnPlayer(plr, Value)
    local model = game.ReplicatedStorage.Car:FindFirstChild(CreateWeapon(Value)) or game.ReplicatedStorage.CarWeapon:FindFirstChild(CreateWeapon(Value))
    local oldModel = plr.Character
    local newModel = model:Clone()
    newModel.Name = plr.Name
    newModel.Parent = workspace.Cars
    plr.Character = newModel
    newModel:FindFirstChildOfClass("Humanoid").WalkSpeed *= CarLevel.GetSpeedMultiplierByExp(Value:GetAttribute("EXP"))
    if plr.Potions.SpeedPotion.Value > 0 then
        newModel:FindFirstChildOfClass("Humanoid").WalkSpeed *= 2
    end
    for i, Object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do
        local Clone = Object:Clone()
        Clone.Parent = newModel
    end
    plr.Team = game.Teams.Cars
    plr.Character:SetPrimaryPartCFrame(workspace.CarsSpawn.CFrame)
    local DiedEvent = script.DiedEvent:Clone()
    DiedEvent.Parent = newModel
    DiedEvent.Enabled = true
    local Gasoline = script.Gasoline:Clone()
    Gasoline.Parent = newModel
    Gasoline.Enabled = true
    DebrisService:AddItem(oldModel, 0.2)

    for i, Enchantment in Value:GetChildren() do
        if Enchantment:IsA("BoolValue") then
            local EnchantmentScript = script:FindFirstChild(Enchantment.Name)
            if EnchantmentScript then
                local Clone = EnchantmentScript:Clone()
                Clone.Parent = newModel
                Clone.Enabled = true
            end
        end
    end
    game.ReplicatedStorage.Events.ShowGasolineGUI:FireClient(plr, plr.UserId)
end

Does it freeze the game for a short period or directly make Roblox unplayable?

If it only takes a short period, please ask one of your friends to test the game as well. The problem could be about your graphics.

Well, I’m going to be honest but it’s hard for me to believe the first script might affect anything in such a kind to result in the game freezing.

On the other side, in the second script, personally, I believe directly replacing plr.Character with a new cloned model, especially without yielding or ensuring the old character is properly cleaned up, MAY cause trouble in the Roblox Player engine. The freeze might be likely due to the reassignment of characters combined with the GUI and remote event firing (to clarify, this might be wrong but I’ve just stated my thoughts)

make the roblox unplayable, it just freeze until you close it

i tryed that, don’t work, and i discovered that better the device, less it happens

it was a virus in the game, i used ro-defender and it stops happen for me