I want to make an if statement testing if a player load character. The issue I keep getting with my if statement is that when I join the game I keep respawning nonstop. Script in ServerScriptService:
game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Char) if Player:LoadCharacter() then--if statement broken wait(6) Player.PlayerGui.MainUI.Enabled = true Player.PlayerGui.MoneyGui.Enabled = true end end) end)
Gui button
game.Players.PlayerAdded:Connect(function(player) player:GetChildren(player) script.Parent.MouseButton1Click:Connect(function() game.ReplicatedStorage.LoadPlayer:FireServer()-- make a remote event named LoadPlayer player:LoadCharacter() end) end)
you can use the Char.Parent property instead of Player:LoadCharacter() to test if the character has been added to the game:
game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Char) if Char.Parent ~= nil then wait(6) Player.PlayerGui.MainUI.Enabled = true Player.PlayerGui.MoneyGui.Enabled = true end end) end)
'LoadCharacter' function is blocking, so it halts the script until the character is fully loaded. This then causes an infinite loop as the character is not fully loaded when the 'if' statement is called
Here a solution try using a remote event
This is an example that your gui button script might be
script.Parent.MouseButton1Click:Connect(function() game.ReplicatedStorage.LoadPlayer:FireServer()-- make a remote event named LoadPlayer --Put your load character code here
Script in serverscriptservice
game.ReplicatedStorage.LoadPlayer.OnServerEvent:Connect(function(plr) wait(6) -- wait here make sure there character is fully loaded --Put your gui stuff here that you want visible
Let me know if any errors NOT tested Hope this helps!!