so i am making a game, not really an obby but something like that, my question is, when a player touched a certain part for example he walked on that part, how can i make that part the certain player’s spawn point so that if he rejoins, he will spawn in that place
Assuming this is an obby and what you mean by “rejoining”, you could do the following:
Place parts where you want the player to spawn in a folder or something like that. Name them efficiently. For example, the first stage spawn name could be “1”, the name of the second stage spawn name could be “2”.
Whenever a player touches one of those spawn points, you can use a IntValue or StringValue inside the player and update it accordingly. Once the player leaves the game, datastore the value.
Once the player rejoins, you’d simply load in that data and change the characters position to the part you’d like.
If you’d like sample code let me know.
can i have a sample please? im new to this and still making my first game^^
Here is an example of how I set up the spawns. As for the code, here is something that would probably work although I haven’t tested it. If your testing this in Studio make sure to go into Game Settings > Security and turn on “Enable Studio Access to API Services”
local DS = game:GetService("DataStoreService")
local SpawnData = DS:GetDataStore("Spawndata")
local SpawnFolder = workspace.SpawnFolder
game.Players.PlayerAdded:Connect(function(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local SpawnValue = Instance.new("StringValue")
SpawnValue.Name = "CurrentCheckpoint"
SpawnValue.Parent = plr
local succ, result = pcall(function()
return SpawnData:GetAsync(plr.UserId)
end)
if succ then
if result ~= nil then
SpawnValue.Value = result
if workspace.SpawnFolder:FindFirstChild(result) then
char.HumanoidRootPart.CFrame = workspace.SpawnFolder:FindFirstChild(result).CFrame
end
end
end
end)
for _, v in pairs(SpawnFolder:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
local plrWhoTouched = game.Players:FindFirstChild(hit.Parent.Name)
plrWhoTouched.CurrentCheckpoint.Value = v.Name
end
end)
end
end
game.Players.PlayerRemoving:Connect(function(plr)
SpawnData:SetAsync(plr.UserId, plr.CurrentCheckpoint.Value)
end)
If you wanted the player to respawn at there spawnpoint when they die as well, you can simply hook up the Humanoid.Died and make it CharacterAdded as well, although you didn’t say you wanted to so I didn’t account for it in this script.
local players = game:GetService("Players")
local spawnFolder = workspace.Folder --Path to folder of spawn parts.
local function onPlayerAdded(player)
local function onCharacterAdded(character)
if not player:HasAppearanceLoaded() then
player.CharacterAppearanceLoaded:Wait()
end
if player.Spawn.Value then
task.wait()
character:PivotTo(player.Spawn.Value:GetPivot())
end
end
player.CharacterAdded:Connect(onCharacterAdded)
local _spawn = Instance.new("ObjectValue")
_spawn.Name = "Spawn"
_spawn.Parent = player
end
players.PlayerAdded:Connect(onPlayerAdded)
local function onSpawnTouched(hitPart, _spawn)
local hitModel = hitPart:FindFirstAncestorOfClass("Model")
if not hitModel then return end
local hitPlayer = players:GetPlayerFromCharacter(hitModel)
if not hitPlayer then return end
hitPlayer.Spawn.Value = _spawn
end
for _, _spawn in ipairs(spawnFolder:GetChildren()) do
if _spawn:IsA("BasePart") then
_spawn.Touched:Connect(function(hitPart)
onSpawnTouched(hitPart, _spawn)
end)
end
end
This script is working on my end, let me know if you need any further assistance.
thank u so much for the help!!!
I used your script with a few modifications but im having a problem with players spawning at the default spawn location
should i just disable that spawnlocation?