I would like the player to spawn at the red spawn if they selected guard, or a grey spawn as a civilian.
No matter what I seem to do, the player only spawns at the black spawn “choosing.” This spawn is not set to neutral, so I’m wondering if its an issue with the teams.
I have ensured the team colors are matching with a spawn. All three spawns are not neutral. I have added a couple lines in the script to ensure the player’s team color and team is changed. I am a beginner in Lua and I have not used any videos or tutorials.
there is a video attached.
here is my script:
-- local mainmenugui = playerGui:WaitForChild("Main Menu Gui")
local play = mainmenugui.Frame.Play
local teamSelect = mainmenugui.Frame.TeamSelect
local gamegui = playerGui:WaitForChild("Game Gui")
local menuButton = gamegui.Frame.Menu
local teamWarning = mainmenugui.Frame["No team selected!"]
play.Activated:Connect(function()
if player.TeamColor ~= BrickColor.new("Black") then
mainmenugui.Enabled = false
gamegui.Enabled = true
player.Character:FindFirstChildOfClass("Humanoid").Health = 0
else
teamWarning.Visible = true
wait(1)
teamWarning.Visible = false
end
end)
menuButton.Activated:Connect(function()
player.Character:FindFirstChildOfClass("Humanoid").Health = 0
player.TeamColor = BrickColor.new("Black")
mainmenugui.Enabled = true
gamegui.Enabled = false
end)
--team selection
local teamPreview = mainmenugui.Frame["Teams Preview"]
local back = mainmenugui.Frame.Back
teamSelect.Activated:Connect(function()
teamPreview.Visible = true
back.Visible = true
play.Visible = false
teamSelect.Visible = false
end)
local teamSelectedWarning = mainmenugui.Frame["Chosen team selected!"]
--select a team
local civTeamSelect = teamPreview["Civilian Team"].TextButton
local guardTeamSelect = teamPreview["Guard Team"].TextButton
civTeamSelect.Activated:Connect(function()
player.TeamColor = BrickColor.new("Medium stone grey")
player.Team = game.Teams["Civilian"]
teamSelectedWarning.Visible = true
wait(1)
teamSelectedWarning.Visible = false
end)
local rankWarning = mainmenugui.Frame["Not in group/rank!"]
guardTeamSelect.Activated:Connect(function()
if player:IsInGroup(455667012) then
player.TeamColor = BrickColor.new("Really red")
player.Team = game.Teams["Guards"]
teamSelectedWarning.Visible = true
wait(1)
teamSelectedWarning.Visible = false
else
rankWarning.Visible =true
wait(1)
rankWarning.Visible = false
end
end)
--back to main menu
back.Activated:Connect(function()
teamPreview.Visible = false
back.Visible = false
play.Visible = true
teamSelect.Visible = true
end)
Updating the Player’s TeamColor property from a LocalScript only changes it from the perspective of that player, which means that won’t be seen by other players & the server.
This means that a Script running on the server-side would need to change the player’s Team / TeamColor; one way of doing that with your current setup would be to use a RemoteEvent on the client-side and then call RemoteEvent:FireServer(), sending through the new BrickColor to the server so that it knows what the player’s TeamColor should be updated to.
Once the server updates the player’s TeamColor, then the player will end up loading at the designated Team spawnpoint(s) the next time they respawn.
Here’s an example of that change (but keep in mind this change would need to be made anywhere in the LocalScript where it tries to change the TeamColor, so this includes all of the functions where the “TeamSelect” buttons are Activated, too).
(Before doing anything, make sure to add a RemoteEvent into the ReplicatedStorage, and name it “TeamChangeRemoteEvent”. If you’d like to name it something different, make sure to also change how it is referenced in the code, since it needs to have the exact same spelling and capitalization for it to find it!)
LocalScript Example
-- Top of the LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeamChangeRemoteEvent = ReplicatedStorage.TeamChangeRemoteEvent
-- all other code from the top of the LocalScript in original post here
menuButton.Activated:Connect(function()
mainmenugui.Enabled = true
gamegui.Enabled = true
TeamChangeRemoteEvent:FireServer(BrickColor.new("Black"))
--[[ RemoteEvent to be fired to the server any time the player presses a button that
wants to update their TeamColor; the server script code below will handle it --]]
end)
Server Script Example
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeamChangeRemoteEvent = ReplicatedStorage.TeamChangeRemoteEvent
TeamChangeRemoteEvent.OnServerEvent:Connect(function(player, newTeamColor)
player.TeamColor = newTeamColor
player:LoadCharacter()
end)
hey man, tysm idk how i didn’t think about that but i have hooked it up to a remote event and used load character instead of health = 0 and now it works.
I had kept it simple since OP mentioned that they were a beginner in Lua but after you mentioned it, ya it’d probably have been better if I included it anyway
In that case, for OP and anyone else reading, it’s important to check the data that the client sends through to the server since it might not always send through what you expect (e.g. if you have a TextBox that lets players submit text, or even in cases where an exploiter tampers with data and sends random stuff through RemoteEvents) then it’s important to have the server check if it’s receiving what you expect it to, or else it could error.
So for this case, here’s 2 different examples of how you could validate the BrickColor the client sent through:
Example 1
This example makes sure that data was actually sent through to the RemoteEvent before trying to update the TeamColor… and while this could work for most cases, Example #2 will be more secure.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeamChangeRemoteEvent = ReplicatedStorage.TeamChangeRemoteEvent
TeamChangeRemoteEvent.OnServerEvent:Connect(function(player, newTeamColor)
if newTeamColor ~= nil then
player.TeamColor = newTeamColor
player:LoadCharacter()
end
end)
Example 2 (better)
This example checks the type of data that was sent through, using typeof(), which is much more specific and helps to REALLY make sure the function received the exact type of data that it needs to work as intended.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeamChangeRemoteEvent = ReplicatedStorage.TeamChangeRemoteEvent
TeamChangeRemoteEvent.OnServerEvent:Connect(function(player, newTeamColor)
local brickColorCheck = typeof(newTeamColor)
if brickColorCheck == "BrickColor" then
player.TeamColor = newTeamColor
player:LoadCharacter()
end
end)