Can't make a spawnpoint on roblox

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)
2 Likes

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.

1 Like

I expect you are trying to get the HRP before the character has fully loaded, try something like this:

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("HumanoidRootPart")
1 Like

Could be, but he already has it wrapped in a CharacterAdded function.

1 Like

@GamerAlex323 Could you add a debug print statement in an else branch of the if humanoidRootPart statement? Just put print(humanoidRootPart, role, role.Value).

1 Like

local Players = game:GetService(“Players”)

local SpawnPart = script.Parent

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)
1 Like

One more addition you could possibly make is use the :GetPropertySignalChanged connection for the role value. It would look something like:

role:GetPropertySignalChanged("Value"):Wait()

instead of the static 0.25 seconds. Also,

Is this meant to be role.Value == nil?

1 Like

Actually yes it is meant to be role.Value == nil but it works with role.Value ~= nil for some reason

1 Like

Interesting. Well, if it works, it works lol. Also, did the :GetPropertySignalChanged work?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.