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:
1 | game.Players.PlayerAdded:Connect( function (Player) |
2 | Player.CharacterAdded:Connect( function (Char) |
3 | if Player:LoadCharacter() then --if statement broken |
4 | wait( 6 ) |
5 | Player.PlayerGui.MainUI.Enabled = true |
6 | Player.PlayerGui.MoneyGui.Enabled = true |
7 | end |
8 | end ) |
9 | end ) |
Gui button
1 | game.Players.PlayerAdded:Connect( function (player) |
2 | player:GetChildren(player) |
3 | script.Parent.MouseButton 1 Click:Connect( function () |
4 | game.ReplicatedStorage.LoadPlayer:FireServer() -- make a remote event named LoadPlayer |
5 | player:LoadCharacter() |
6 | end ) |
7 |
8 |
9 | end ) |
you can use the Char.Parent property instead of Player:LoadCharacter() to test if the character has been added to the game:
1 | game.Players.PlayerAdded:Connect( function (Player) |
2 | Player.CharacterAdded:Connect( function (Char) |
3 | if Char.Parent ~ = nil then |
4 | wait( 6 ) |
5 | Player.PlayerGui.MainUI.Enabled = true |
6 | Player.PlayerGui.MoneyGui.Enabled = true |
7 | end |
8 | end ) |
9 | 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
1 | script.Parent.MouseButton 1 Click:Connect( function () |
2 | game.ReplicatedStorage.LoadPlayer:FireServer() -- make a remote event named LoadPlayer |
3 | --Put your load character code here |
Script in serverscriptservice
1 | game.ReplicatedStorage.LoadPlayer.OnServerEvent:Connect( function (plr) |
2 | wait( 6 ) -- wait here make sure there character is fully loaded |
3 | --Put your gui stuff here that you want visible |
Let me know if any errors NOT tested Hope this helps!!