Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why it spawns me on a wrong location when I spawned in?

https://gyazo.com/25936ae2ba1c6fe39f9b8d8846768bd2

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("StageSaveSystem")

game.Players.PlayerAdded:connect(function(player)
    local leader = Instance.new("Folder",player)
    leader.Name = "leaderstats"
    local Stage = Instance.new("IntValue",leader)
    Stage.Name = "Stage"
    Stage.Value = ds:GetAsync(player.UserId)
    if Stage.Value == nil then Stage.Value = 0 end
    player.CharacterAdded:Connect(function(character)
        local spaw = workspace:FindFirstChild(tostring(Stage.Value))
        spaw.Enabled = true 
        wait()
        spaw.Enabled = false
    end)

    ds:SetAsync(player.UserId, Stage.Value)
    
    Stage.Changed:connect(function()
        ds:SetAsync(player.UserId, Stage.Value)
    end)
    
end)

game.Players.PlayerRemoving:connect(function(player)
    ds:SetAsync(player.UserId, player.leaderstats.Stage.Value)
end)

local stage = 10


function ot(hit)
	if hit.Parent ~= nil then
	local player = game.Players:playerFromCharacter(hit.Parent)
		if player ~= nil then
			if player.leaderstats.Stage.Value == stage - 1  then
				local h = hit.Parent:FindFirstChild("Humanoid")
				if h ~= nil then
					if h.Health ~= 0 then
						player.leaderstats.Stage.Value = stage
					end 
				end
			end
		end
	end
end

script.Parent.Touched:connect(ot)

1 answer

✓ Accepted answer

You are probably better off changing the player’s RespawnLocation when their Stage changes instead of changing the Enabled property of the spawns.

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("StageSaveSystem")

game.Players.PlayerAdded:connect(function(player)
    local leader = Instance.new("Folder",player)
    leader.Name = "leaderstats"
    local Stage = Instance.new("IntValue",leader)
    Stage.Name = "Stage"
    Stage.Value = ds:GetAsync(player.UserId)
    if Stage.Value == nil then Stage.Value = 0 end
    player.RespawnLocation = workspace:FindFirstChild(tostring(Stage.Value))


    ds:SetAsync(player.UserId, Stage.Value)

    Stage.Changed:connect(function()
        ds:SetAsync(player.UserId, Stage.Value)
	player.RespawnLocation = workspace:FindFirstChild(tostring(Stage.Value))
    end)

end)

game.Players.PlayerRemoving:connect(function(player)
    ds:SetAsync(player.UserId, player.leaderstats.Stage.Value)
end)

This is assuming you have the script to change the leaderstat when you touch the part, and that the spawns are named after the appropriate Stage numbers.

This discussion is preserved as it was. New answers, comments, and votes are closed.