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

LoadCharacter() takes character to wrong spawn after reloading character?

I'm trying to make a Menu GUI where a player can select their team. It's a really simple GUI and literally just fires a RemoteEvent that switches their team and reloads the player. In Studio it works and it transports them to the right team spawn but in-game it only half works. It changes the player's team and reloads them but they spawn at the completely wrong spawn (which I assume is just the default spawnpoint). Neutral is disabled on all of the spawnpoints and at this point I'm really confused why the player is not being transported to the right spawn like in studio.

GUI Script:

local rStorage = game:GetService("ReplicatedStorage")
local remote = rStorage:WaitForChild("removegui")

function CheckTeams()
	if script.Parent.Parent.Parent.Parent.Parent:GetRankInGroup(#######) >= 0 then
		script.Parent.Parent.Parent.Parent.Parent.TeamColor = BrickColor.new("Dark stone grey")
		remote:FireServer()
		script.Parent.parent.Parent.Parent.ACRMenu.Enabled = false
	end
end

script.Parent.MouseButton1Click:Connect(CheckTeams)

Server Script:

local rStorage = game:GetService("ReplicatedStorage")
local remote = rStorage:WaitForChild("removegui")

remote.OnServerEvent:connect(function(player)
	player:LoadCharacter()
end)

1 comment

0

Are the spawns for the right team

1 answer

Firstly, I would advise using game.Players.LocalPlayer rather than script.Parent.Parent... as it would work, even if you moved the script up the hierarchy.

Secondly, I think I understand why this doesn't work ingame. From what I understand, you cannot change a player's team from a LocalScript; the server doesn't recognise that change because of FE, so when you respawn, it still teleports you to your original team's spawn. You would put all the code in the server script.

LocalScript

local rStorage = game:GetService("ReplicatedStorage")
local remote = rStorage:WaitForChild("removegui")

function CheckTeams()
	remote:FireServer()
end

script.Parent.MouseButton1Click:Connect(CheckTeams)

Server Script

local rStorage = game:GetService("ReplicatedStorage")
local remote = rStorage:WaitForChild("removegui")

remote.OnServerEvent:Connect(function(player)
	if player:GetRankInGroup(#######) >= 0 then -- It's better if the server checks the rank, or a hacker would just fire the RE themselves.
		player.TeamColor = BrickColor.new("Dark stone grey")
		player:LoadCharacter()
	end
	
end)

Does this work?

1 comment

0

Yeah, this should be the correct way to do it. As a safety in case, I tend to also put the team in there with the TeamColor.

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