Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do i do a checkpoint that saves after the player exits?

Asked by 4 years ago

Sorry for the bad English, It's not my native language. I'm trying to do a obby,and for that i wanted to do a checkpoint that saves after the player exits the map. To do that,I researched on some websites.I found two old scripts and the official roblox site that explained the data store(https://developer.roblox.com/articles/Saving-Player-Data). The problem is that i didn't undestand how to use the data store script,and the old script didn't work anymore. The Data store script


local PlayerStatManager = {} local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData") local sessionData = {} local AUTOSAVE_INTERVAL = 60 function PlayerStatManager:ChangeStat(player, statName, value) assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match") local playerUserId = "Player_" .. player.UserId if typeof(sessionData[playerUserId][statName]) == "number" then sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value else sessionData[playerUserId][statName] = value end end local function setupPlayerData(player) local playerUserId = "Player_" .. player.UserId local success, data = pcall(function() return playerData:GetAsync(playerUserId) end) if success then if data then -- Data exists for this player sessionData[playerUserId] = data else -- Data store is working, but no current data for this player sessionData[playerUserId] = {Money=0, Experience=0} end else warn("Cannot access data store for player!") end end local function savePlayerData(playerUserId) if sessionData[playerUserId] then local success, err = pcall(function() playerData:SetAsync(playerUserId, sessionData[playerUserId]) end) if not success then warn("Cannot save data for player!") end end end local function saveOnExit(player) local playerUserId = "Player_" .. player.UserId savePlayerData(playerUserId) end local function autoSave() while wait(AUTOSAVE_INTERVAL) do for playerUserId, data in pairs(sessionData) do savePlayerData(playerUserId) end end end spawn(autoSave) game.Players.PlayerAdded:Connect(setupPlayerData) game.Players.PlayerRemoving:Connect(saveOnExit) return PlayerStatManager

The two old scripts

function oa(object)
    local player = game.Players:playerFromCharacter(object)
    if player ~= nil then
        local ls = player.leaderstats
        local sl = game.Workspace:FindFirstChild(ls.Stage.Value)
        print("obby")
        object.Torso.CFrame = object.Torso.CFrame + Vector3.new(0,3,0)
        wait()
        object.Torso.CFrame = sl.CFrame + Vector3.new(0,3,0)
    end 
end

function oe(object)
    if object.className == "Player" then
        local ack = Instance.new("IntValue")
        ack.Name = "leaderstats"
        local ack2 = Instance.new("IntValue")
        ack2.Name = "Stage"
        ack2.Value = 1
        ack2.Parent = ack
        ack.Parent = object
    end 
end

game.Players.ChildAdded:connect(oe)
game.Workspace.ChildAdded:connect(oa)

this one goes on the block.

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

script.Parent.Touched:connect(ot)

when the old ones run appears the error Torso is not a valid member of Model,so I think the problem is that i'm running the script on R15,so i tried changing the Torso to Humanoid RootPart,But i don't know how to put the HumanoidRootPart in the script since it's not in the StarterCharacterScripts or StarterPlayerScripts So I wanted to know how to use the data store script and how to fix the old scripts,or just a better way to do a checkpoint that saves after the player exits.

0
dont steal roblox wiki code, instead make ur own xd greatneil80 2647 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

this is the script

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("GemSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "leaderstats"
 local stage = Instance.new("IntValue", folder)
 stage.Name = "Stage"

 stage.Value = ds1:GetAsync(plr.UserId) or 1
 ds1:SetAsync(plr.UserId, stage.Value)

 stage.Changed:connect(function()
  ds1:SetAsync(plr.UserId, stage.Value)
 end)
end)

put the script in Workspace

Ad

Answer this question