Script sometimes work, sometimes doesnt

	SpawnPart1 = workspace:WaitForChild("benespawn");
	SpawnPart2 = workspace:WaitForChild("MENUSPAWB");
	SpawnPart3 = workspace:WaitForChild("citizenspawn");
	SpawnPart4 = workspace:WaitForChild("rebelspawn");
	SpawnPart5 = workspace:WaitForChild("cwuspawn");
}
local GroupID = 16221838

game.Players.PlayerAdded:Connect(function(player)
	if player:GetRankInGroup(GroupID) ~= 255 then return end
	player.CharacterAdded:Connect(function(char)
		print("Hey")
		if player.Team.Name == "Combine" and player:GetRankInGroup(GroupID) == 255 then char:PivotTo(spawns[string.format("SpawnPart1", player.Team.Name == "Combine")]:GetPivot() * CFrame.new(0, 2.5, 0))
		elseif player.Team.Name == "Menu" then char:PivotTo(spawns[string.format("SpawnPart2", player.Team.Name == "Menu")]:GetPivot() * CFrame.new(0, 2.5, 0))
			print("GG")
		elseif player.Team.Name == "Citizens" then char:PivotTo(spawns[string.format("SpawnPart3", player.Team.Name == "Citizens")]:GetPivot() * CFrame.new(0, 2.5, 0))
		elseif player.Team.Name == "CWU" then char:PivotTo(spawns[string.format("SpawnPart5", player.Team.Name == "CWU")]:GetPivot() * CFrame.new(0, 2.5, 0))
			print("Ht")
		elseif player.Team.Name == "Rebel" then char:PivotTo(spawns[string.format("SpawnPart4", player.Team.Name == "Rebel")]:GetPivot() * CFrame.new(0, 2.5, 0))
			print("Here")
		end
	end)
end)

this script is for a group team rank spawn, it used to work fine, but now it works only sometimes, and most of the time doesnt work

2 Likes

Sometimes the character spawns before .CharacterAdded can register it. Try

if player.Character then
-- print("Hey") etc

then connect the character added too

2 Likes

Maybe try waiting for the character to load before you do the team stuff

1 Like

Specifically like this.

local function doThing(char)
    print("Hey")
	if player.Team.Name == "Combine" and player:GetRankInGroup(GroupID) == 255 then char:PivotTo(spawns[string.format("SpawnPart1", player.Team.Name == "Combine")]:GetPivot() * CFrame.new(0, 2.5, 0))
	elseif player.Team.Name == "Menu" then char:PivotTo(spawns[string.format("SpawnPart2", player.Team.Name == "Menu")]:GetPivot() * CFrame.new(0, 2.5, 0))
			print("GG")
	elseif player.Team.Name == "Citizens" then char:PivotTo(spawns[string.format("SpawnPart3", player.Team.Name == "Citizens")]:GetPivot() * CFrame.new(0, 2.5, 0))
	elseif player.Team.Name == "CWU" then char:PivotTo(spawns[string.format("SpawnPart5", player.Team.Name == "CWU")]:GetPivot() * CFrame.new(0, 2.5, 0))
			print("Ht")
	elseif player.Team.Name == "Rebel" then char:PivotTo(spawns[string.format("SpawnPart4", player.Team.Name == "Rebel")]:GetPivot() * CFrame.new(0, 2.5, 0))
		print("Here")
	end
end

local character = player.Character
if character then doThing(character) end
player.CharacterAdded:Connect(doThing)
1 Like

Additionally, there’s a better way to do this. You can directly assign char and wait for it to be added.

-- insert code above
game.Players.PlayerAdded:Connect(function(player)
	if player:GetRankInGroup(GroupID) ~= 255 then return end
    -- replace the :Connect() function with this
    local char = player.Character or player.CharacterAdded:Wait() 
    print("Hey")
    -- insert code below
1 Like
game.Players.PlayerAdded:Connect(function(player)
	if player:GetRankInGroup(GroupID) ~= 255 then return end
	local char = player.Character or player.CharacterAdded:Wait() 
	print("Hey")
	if player.Team.Name == "Combine" and player:GetRankInGroup(GroupID) == 255 then char:PivotTo(spawns[string.format("SpawnPart1", player.Team.Name == "Combine")]:GetPivot() * CFrame.new(0, 2.5, 0))
	elseif player.Team.Name == "Menu" then char:PivotTo(spawns[string.format("SpawnPart2", player.Team.Name == "Menu")]:GetPivot() * CFrame.new(0, 2.5, 0))
		print("GG")
	elseif player.Team.Name == "Citizens" then char:PivotTo(spawns[string.format("SpawnPart3", player.Team.Name == "Citizens")]:GetPivot() * CFrame.new(0, 2.5, 0))
	elseif player.Team.Name == "CWU" then char:PivotTo(spawns[string.format("SpawnPart5", player.Team.Name == "CWU")]:GetPivot() * CFrame.new(0, 2.5, 0))
		print("Ht")
	elseif player.Team.Name == "Rebel" then char:PivotTo(spawns[string.format("SpawnPart4", player.Team.Name == "Rebel")]:GetPivot() * CFrame.new(0, 2.5, 0))
		print("Here")
	end
end)

not sure if i did it right, because this doesn’t seem to work either

2 Likes

Well, if the previous solution works for you then you should go with that.

1 Like

Now that I read the script it doesn’t make much sense

1 Like

the script did use to work, and one day it just randomly broke

1 Like

Are spawn parts actual parts or is it models?

1 Like
local Players = game:GetService("Players")

local SpawnParts = {
	SpawnPart1 = workspace:WaitForChild("benespawn");
	SpawnPart2 = workspace:WaitForChild("MENUSPAWB");
	SpawnPart3 = workspace:WaitForChild("citizenspawn");
	SpawnPart4 = workspace:WaitForChild("rebelspawn");
	SpawnPart5 = workspace:WaitForChild("cwuspawn");
}
local TeamToSpawn = {
	["Combine"] = SpawnParts.SpawnPart1,
	["Menu"] = SpawnParts.SpawnPart2,
	["Citizens"] = SpawnParts.SpawnPart3,
	["Rebel"] = SpawnParts.SpawnPart4,
	["CWU"] = SpawnParts.SpawnPart5
}
local GroupID = 16221838

local function teleportToSpawn(character, teamName)
	local spawnPart = TeamToSpawn[teamName]
	if not spawnPart then spawnPart = TeamToSpawn.Menu end -- default spawn part
	
	local cframe = spawnPart:GetPivot() * CFrame.new(0, 2.5, 0)
	character:SetPrimaryPartCFrame(cframe)
end

local function onPlayerAdded(player)
	if player:GetRankInGroup(GroupID) < 255 then return end -- owner-only?
	
	local character = player.Character
	if character then teleportToSpawn(character, player.Team.Name) end
	player.CharacterAdded:Connect(function(char)
		teleportToSpawn(char, player.Team.Name)
	end)
end
Players.PlayerAdded:Connect(onPlayerAdded)

Could you be more descriptive when you are saying it ‘doesn’t work’? Are there any specific examples with certain players which causes it to ‘not work’ as you intended it? You could also give pointers as to what actually isn’t working instead of putting an excerpt of one script and saying it ‘doesn’t work’

it literally just doesn’t do anything anymore, it prints, nothing else, it used to work

actually correction, it does work, sometimes, its a once in a while thing i see the script actually carrys out what its meant to do