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

Data Storing a CFrame value?

So I've been working on a Roblox RPG and for four days I've been trying to make a save and load system. But I've had no success. I'm trying to use a Script in ServerScriptService to give the player a CFrame value and when the player presses a save button, the CFrame updates and when they leave it stays the same. I've looked all over to find any answer but no success in what I'm trying to do. So far this is what I have in my DataStore Script.

local DataStore = game:GetService("DataStoreService")
local save = DataStore:GetDataStore("SavePos")

game.Players.PlayerAdded:Connect(function(plr)
	local spot = Instance.new("Folder",plr)
	spot.Name = "SaveLocation"
	
	local pos = Instance.new("CFrameValue",spot)
	pos.Name = "PlayerPosition"
end)

This is all I have, there used to be more but I deleted it since it wouldn't work. Any help is appreciated, thank you.

EDIT - So with the buttons I've made a little test to get the buttons from PlayerGui,

local DataStore = game:GetService("DataStoreService")
local save = DataStore:GetDataStore("SavePos")
local saveButton = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton
local loadButton = game.Players.LocalPlayer.PlayerGui.ScreenGui.OtherButton

game.Players.PlayerAdded:Connect(function(plr)
    local spot = Instance.new("Folder",plr)
    spot.Name = "SaveLocation"

    local pos = Instance.new("CFrameValue",spot)
    pos.Name = "PlayerPosition"

    pos.Value = save:GetAsync(plr.UserId) or 0

	saveButton.MouseButton1Click:Connect(function()
		
	end)
	
	loadButton.MouseButton1Click:Connect(function()
		
	end)

end)

Now I want it to update pos.Value when saveButton is clicked on the HumanoidRootPart.CFrame, then maybe save could be used, not sure how really though, DataStores are not my strong suite. Then when you leave and come back, pressing loadButton would then load your position etc.

3 comments

1

This system can be very problematic if the map changes. A player could be positioned within a house that changes position. I would just spawn the player at the closes spawn position ingame.

0

So make a system where when the player leaves, he'll rejoin at the same spawn?

0

dude,save the value as an int,then just use that int inside the CFrame,like CFrame.new(num),

1 answer

listen for when the button is clicked and fire to the server to save the position of the player. also the parent argument of Instance.new() is deprecated, set the parent last.

local DataStore = game:GetService("DataStoreService")
local save = DataStore:GetDataStore("SavePos")
local rm = game.ReplicatedStorage.RemoteEvent -- mayb this was the remote event

local function updateData(plr) -- update data
	local key = "id_"..plr.UserId
	
	pcall(function()
		save:UpdateAsync(key, function(old)
			return plr.SaveLocation.PlayerPosition.Value or old -- return the new value or the old one
		end)
	end)
end

game.Players.PlayerAdded:Connect(function(plr)
    local spot = Instance.new("Folder")
	local key = "id_"..plr.UserId
	
    spot.Name = "SaveLocation"
	spot.Parent = plr

    local pos = Instance.new("CFrameValue")
    pos.Name = "PlayerPosition"
	pos.Parent = spot

	local getPos = save:GetAsync(key)
	
	if getPos then
		local char = plr.Character or plr.CharacterAdded:Wait()
		
		pos.Value = getPos
		char:SetPrimaryPartCFrame(pos.Value)
	end
	
	local canSave = true
	
	rm.OnServerEvent:Connect(function(player, savePosition) -- listen for when the client clicks the save button
		if savePosition == "savePosition" and canSave then
			updateData(player) -- update the players pos
			print("saved!")
			canSave = false
			wait(10) -- to prevent it from updating the data too much which results in overloading the queue
			canSave = true
		end
	end)
end)

client

local button = script.Parent -- define where the button is
local rm = game.ReplicatedStorage.RemoteEvent

button.Activated:Connect(function()
	rm:FireServer("savePosition")
end)

9 comments

0

and to clarify the client, (I tried remote functions like this before) I'd put the client script in a LocalScript IN the save button?

0

sure

0

also its remote events, remote functions are for returning values

0

Yup got that I'll let you know if this works, thanks for all your help so far.

0

changed something

0

forgot to set the players position to the value when they join

0

theres some bugs im trying to fix

0

Ok so I'm getting the print("Saved") so I know it saved, but how would I go about loading it, am I missing something in one of the scripts, I'll keep reading through it.

0

ya im fixing that currently

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