Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you use Load Character for a start menu? (How to respawn your character)

I am trying to make a start screen. I have made it so you change teams, but when I click continue I need to respawn the character so it will spawn in the designated area. The problem is, I keep getting this error: 19:07:15.090 - ServerScriptService.CloseStarterGui:2: attempt to index nil with 'LoadCharacter'

My Local Script code:

local button = script.Parent
local cont = false

local function onButtonActivated()
	if game.Players.LocalPlayer.TeamColor ~= BrickColor.new("Really black") then
		if cont == false then
			cont = true
			wait(.1)
			game.ReplicatedStorage.CloseStarterGui:FireServer()
			script.Parent.Parent.Enabled = false
		else
			warn("Stop spamming continue!") --to make sure the script isn't run more than once
		end
		
	else
		if script.Parent.Parent.ImageLabel.ChooseATeam.Visible == false then --If they didn't choose a team it will tell them to choose one.
			script.Parent.Parent.ImageLabel.ChooseATeam.Visible = true
			wait(1)
			script.Parent.Parent.ImageLabel.ChooseATeam.Visible = false
		end
	end
end
button.MouseButton1Click:Connect(onButtonActivated) --to activate the script

My Server script's code (In ServerScriptService):

game.ReplicatedStorage.CloseStarterGui.OnServerEvent:Connect(function()
	game.Players.LocalPlayer:LoadCharacter() --If I am not mistaking, you need to load the character in a normal Script (not a LocalScript)
end)

The reason I am trying to use LoadCharacter instead of setting the player's health to 0 is I am also using ACS, so when someone clicks continue their screen would go black with blood marks on the sides. (not what I want to happen when they click continue (play). Please help.

2 answers

✓ Accepted answer

Server scripts cannot index a "Local player" (game.Players.LocalPlayer). However, the OnServerEvent has a first parameter which is the player instance so luckily you can just replace the server code like this:

game.ReplicatedStorage.CloseStarterGui.OnServerEvent:Connect(function(player) -- the player object is always the first parameter here as we named it player as you can see
    player:LoadCharacter()
end)

1 comment

0

Thank you.

LocalPlayer is only available for LocalScripts. Instead, try getting the player via the event.

local event = game.ReplicatedStorage:WaitForChild('CloseStarterGui')

local function onFired(plr)
	plr:LoadCharacter()
end

event.OnServerEvent:Connect(onFired)

And as for your first script, don't change anything in that.

This discussion is preserved as it was. New answers, comments, and votes are closed.