What i want is a script that makes the player spawn at the position i have put the part at if they have the role displayed on the leaderboard.
The issue here is that when the player first joins the server, they don’t spawn there like i wrote in the script, but they instead spawn in like the default spawn for the game or roblox. This is only when the player first joins though, they spawn at the part when they respawn.
I have tried putting a spawnpoint there and doing player.RespawnLocation but it still gives me the same issue if i have more spawn locations in the game.
local Player = game:GetService("Players")
local spawn = script.Parent
Player.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local humanoidRootPart = char:FindFirstChild("HumanoidRootPart")
local leaderstats = player:WaitForChild("leaderstats")
local role = leaderstats:FindFirstChild("Role")
if humanoidRootPart and role and role.Value == "Owner" then
char:PivotTo(spawn.CFrame)
end
end)
end)
Hey there. Could you try adding task.wait(0.25) right at the start of the CharacterAdded function? It could possibly be due to a delay in Roblox setting the player’s position to the spawn location.
@GamerAlex323 Could you add a debug print statement in an else branch of the if humanoidRootPart statement? Just put print(humanoidRootPart, role, role.Value).
Players.PlayerAdded:Connect(function(player)
local char = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = char:FindFirstChild("HumanoidRootPart")
local leaderstats = player:WaitForChild("leaderstats")
local role = leaderstats:FindFirstChild("Role")
if not humanoidRootPart then
warn(`no HPR found for player {tostring(player.Name)}`)
end
if not role or not role.Value then
warn(`Role is not a string value, got {typeof(role)}`)
end
print(role.Value)
if humanoidRootPart and (role and role.Value == "Owner") then
print("moved")
char:PivotTo(SpawnPart.CFrame)
else
warn("falied to move")
end
end) this code should provide some output if stuff fails, the reason your original code didn’t work is because you might have not initialized the Role’s value where you create it(in some leaderstats script)
it prints humanoidroopart, role, and nothing more so i guess role doesn’t have the value when the player first joins and that is causing the issue. i’ve found that task.wait(0.25) works to solve this, but when the player respawns, they respawn in the default spawn location first then spawn in the spawn i set 0.25 seconds later which i didn’t want so i made a new script which works with what i wanted.
local Player = game:GetService("Players")
local spawn = script.Parent
Player.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local humanoidRootPart = char:FindFirstChild("HumanoidRootPart")
local leaderstats = player:WaitForChild("leaderstats")
local role = leaderstats:FindFirstChild("Role")
if not role or role.Value ~= nil then
task.wait(0.25)
end
if humanoidRootPart and role and role.Value == "Owner" then
char:PivotTo(spawn.CFrame)
else
print(humanoidRootPart, role, role.Value)
end
end)
end)