So I am trying to make a system which is simular to a obby game, where you touch a spawn point and your “stage” goes up by 1 each time you touch a spawnpoint which has a higher value, this works however respawning or spawning people on said spawn points (with baseparts) proves difficult.
Basically what I’ve tried to do is call a function on the player’s character in a “for in pairs do” loop and use the checkpoint that belongs to the player’s stage number to decide where the player is sent to.
However, this does not work.
for _,checkpoint in pairs(parts) do
checkpoint.Touched:Connect(function(hit)
local stage = checkpoint.Name
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and plr.leaderstats and plr.leaderstats.Stage.Value == stage - 1 then
plr.leaderstats.Stage.Value = stage
end
plr.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
hum.Died:Connect(function()
if plr:WaitForChild("Character") then
plr.Character.PrimaryPart.CFrame = checkpoint.CFrame + Vector3.new(0,3,0)
end
end)
end)
end)
end
Is it the positioning of the character function? Or can it not be inside any loop at all?
-- ServerScript
local checkpoints = workspace:FindFirstChild("Checkpoints") --put checkpoints here
for _, checkpoint in pairs(checkpoints) do
checkpoint.Touched:Connect(function(hit)
local stage = checkpoint.Name
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and plr.leaderstats and plr.leaderstats.Stage.Value == stage - 1 then
plr.leaderstats.Stage.Value = stage
end
end)
end
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = plr:WaitForChild("leaderstats")
plr.CharacterAdded:Connect(function(chr)
if leaderstats:FindFirstChild("Stage") then
-- this will get the checkpoint of the current stage
local checkpoint = checkpoints:FindFirstChild(tostring(leaderstats.Stage.Value))
-- teleport player to current stage
chr.HumanoidRootPart.CFrame = checkpoint.CFrame + Vector3.new(0,3,0)
end
end)
end)
so i left out the rest of the script thinking it wasnt relevant
it very much was
local datastore = game:GetService("DataStoreService"):GetDataStore("stages")
local parts = game.Workspace.parts:GetChildren()
game.Players.PlayerAdded:Connect(function(plr)
local ls = Instance.new("Folder",plr)
ls.Name = "leaderstats"
local s = Instance.new("IntValue",ls)
s.Name = "Stage"
s.Value = 0
local data = datastore:GetAsync("stages")
if data then
s.Value = data
end
local hum = plr:WaitForChild("Humanoid")
hum.Died:Connect(function()
local stage = plr.leaderstats.Stage.Value
local success, err = pcall(function()
datastore:SetAsync("stages", stage)
end)
if not success then
warn(err)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local s = plr.leaderstats.Stage
local success, err = pcall(function()
datastore:SetAsync("stages", s.Value)
end)
if not success then
warn(err)
end
end)
for _,checkpoint in pairs(parts) do
checkpoint.Touched:Connect(function(hit)
local stage = checkpoint.Name
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and plr.leaderstats and plr.leaderstats.Stage.Value == stage - 1 then
plr.leaderstats.Stage.Value = stage
end
plr.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
hum.Died:Connect(function()
wait(6)
if plr:WaitForChild("Character") then
plr.Character.PrimaryPart.CFrame = checkpoint.CFrame + Vector3.new(0,3,0)
end
end)
end)
end)
end
there was, but i resolved them and ended up with this
local datastore = game:GetService("DataStoreService"):GetDataStore("stages")
local parts = game.Workspace.parts:GetChildren()
game.Players.PlayerAdded:Connect(function(plr)
local ls = Instance.new("Folder",plr)
ls.Name = "leaderstats"
local s = Instance.new("IntValue",ls)
s.Name = "Stage"
s.Value = 0
local data = datastore:GetAsync("stages")
if data then
s.Value = data
end
plr.CharacterAdded:Connect(function(char)
for _,checkpoint in pairs(parts) do
checkpoint.Touched:Connect(function(hit)
local stage = checkpoint.Name
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr and plr.leaderstats and plr.leaderstats.Stage.Value == stage - 1 then
plr.leaderstats.Stage.Value = stage
end
end)
plr.CharacterAdded:Connect(function(char)
char.PrimaryPart.CFrame = checkpoint.CFrame + Vector3(0,2.5,0)
end)
end
local hum = plr.Character:WaitForChild("Humanoid")
hum.Died:Connect(function()
local stage = plr.leaderstats.Stage.Value
local success, err = pcall(function()
datastore:SetAsync("stages", stage)
end)
if not success then
warn(err)
end
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local s = plr.leaderstats.Stage
local success, err = pcall(function()
datastore:SetAsync("stages", s.Value)
end)
if not success then
warn(err)
end
end)
basically it said that plr.characteradded was nil, thats because it was outside of the playeradded function, so i fixed that