Buying system that saves

what im trying to do is make a buying system in the script where it checks if the player has enough to buy the part

what im questioning tho is how to save that information?

rep.RemoteTools.Sans.Pacfistsans.OnServerEvent:Connect(function(plr)
	sansPacfistmode(plr)
end)
5 Likes

You can save the information by using the DataStore. However, the datastore can only be accessed by the server, not the client.

3 Likes

what im trying to say is

there will be a remote event that fires

once the remote event is catched, it checks if they have enough to buy it

rep.RemoteTools.Sans.Pacfistsans.OnServerEvent:Connect(function(plr)
	if weakbitsans == true then
		print("you own this character")
	elseif plr.leaderstats.Exp.Value == 100 or plr.leaderstats.Exp.Value > 100 then
		local data = main:GetAsync(plr.UserId)
		data.Exp = data.Exp - 100
		sansPacfistmode(plr)
	end
end)

how do i save the function

im not sure how to use the data store function, does this work?

rep.RemoteTools.Sans.Pacfistsans.OnServerEvent:Connect(function(plr)
	local data = mainstore:GetAsync(plr.UserId)
	if data.weakbitsans == true then
		sansPacfistmode(plr)
	elseif plr.leaderstats.Exp.Value == 100 or plr.leaderstats.Exp.Value > 100 then
		local data = mainstore:GetAsync(plr.UserId)
		plr.leaderstats.Exp = plr.leaderstats.Exp - 100
		weakbitsans = true
		mainstore:SetAsync(weakbitsans)
		sansPacfistmode(plr)
	end
end)

SetAsync needs a key, and a bunch of stuff, recommend checking the creator hub’s learn section.

For something much more direct,

may i ask why it says it might be deleted if nothing happens in 30 days!?!?

This function sets the latest value, UserIds, and metadata for the given key.

Values in data stores are versioned, meaning GlobalDataStore:SetAsync() will create a new version every time it is called. Prior versions can be accessed through DataStore:ListVersionsAsync()/DataStore:GetVersionAsync() for up to 30 days at which point they are permanently deleted.

i looked in the documents and found some helpfull information, do you think this will work?

rlocal rep = game.ReplicatedStorage
local datastore = game:GetService("DataStoreService")
local mainstore = datastore:GetDataStore("MainDataStuff")
local weakbitsans = false
local savedata = {

	["abitweaksans"] = false,
	["Lastbreathsans"] = false,
}

rep.RemoteTools.Sans.Weaksans.OnServerEvent:Connect(function(plr)
	local leaderstats = plr:FindFirstChild("leaderstats")
	if leaderstats then
		local data = mainstore:GetAsync(plr.UserId)
		if weakbitsans == data then
			print("You already own this character")
			sansWeakmode(plr)
		elseif leaderstats.Exp.Value == 100 or leaderstats.Exp.Value > 100 then
			print("Buying Character....")
			leaderstats.Exp.Value = leaderstats.Exp.Value - 100
			weakbitsans = true
			mainstore:SetAsync(plr, weakbitsans)
			sansWeakmode(plr)
		else
			print("Haha your too broke")
		end
	end
end)

i got this error, what does it mean

DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = Instance 

Data stores need an key and a scope and value

First you acces the datastore service

local datastore = game:GetService("DataStoreService")

Then we need the scope by doing

local scope = datastore:GetDatastore("scopeName_1") -- change 1 to 2 and you reset the data for all players etc

There comes the point of setAsynch and getAsynch

Firstly we need to use getAsynch we need a key for this what mostly is the players user id but this can be any string

local data = scope: getAsync ("key_or_plr_userId") -- this the key we also use for setAsync

If data == nil then --we need to create the defould data first when there isn't
Scope:setAsync(key_or_plr_userId, "this can be numbers, bools, tables or strings") -- for tables you only may use strings bools and numbers cuz it needs to match Json)
data = scope: getAsync("key_or_plr_userId") -- set the data to just saved valeu
end

Ok now we have the data and setter up the data store if it won’t already exist

Now we want to save I recommend to do this with an cooldown (to much will fail the data store) or on player leave (rn I’m testing this with an pcall when it fails to save it dents the error massage to discord so I know when it fails but Idk I can fail on leave)

local function Save()
scope:setAsync("key_or_plr_userId", "data to save can be string, tables, numbers or bools") -- tables needs to match Json no things like vector3 or instances
end
1 Like

so like this?

rep.RemoteTools.Sans.Weaksans.OnServerEvent:Connect(function(plr)
	local leaderstats = plr:FindFirstChild("leaderstats")
	if leaderstats then
		local data = mainstore:GetAsync(plr.UserId)
		if datastore["bitweaksans"] then
			print("You already own this character")
			sansWeakmode(plr)
		elseif leaderstats.Exp.Value == 100 or leaderstats.Exp.Value > 100 then
			print("Buying Character....")
			leaderstats.Exp.Value = leaderstats.Exp.Value - 100
			weakbitsans = true
			mainstore:SetAsync(plr, true)
			sansWeakmode(plr)
		else
			print("Haha your too broke")
		end
	end
end)


I see you probably use an iligal value can you send the full script what’s the datastore variable

1 Like

Here you use the frong key it needs to match what you used for getAsync and did you loaded in the new data if it’s empty

1 Like

uhh heres the full script, did i do anything wrong?

local rep = game.ReplicatedStorage
local datastore = game:GetService("DataStoreService")
local mainstore = datastore:GetDataStore("MainDataStuff")
local weakbitsans = false
local savedata = {
	
	["abitweaksans"] = false,
	["Lastbreathsans"] = false,
}



local function sansPacfistmode(plr, model)
	local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("Sans")
	local oldModel = plr.Character
	local newModel = model:Clone()
	newModel.Name = plr.Name
	plr.Character = newModel
	newModel.Parent = game.Workspace 
	newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
	for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
		local newObject = object:Clone()
		newObject.Parent = newModel
	end
	local tools = game.ReplicatedStorage.Tools.SansWeakTools:GetChildren()
	for _, tool in ipairs(tools) do
		local clonedTool = tool:Clone()
		clonedTool.Parent = plr.Backpack
	end
	oldModel:Destroy()
end

local function LastBreath(plr, model)
	local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("LastBreathSans")
	local oldModel = plr.Character
	local newModel = model:Clone()
	newModel.Name = plr.Name
	plr.Character = newModel
	newModel.Parent = game.Workspace 
	newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
	for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
		local newObject = object:Clone()
		newObject.Parent = newModel
	end
	local tools = game.ReplicatedStorage.Tools.LastBreathTools:GetChildren()
	for _, tool in ipairs(tools) do
		if tool:IsA("Tool") then
			local clonedTool = tool:Clone()
			clonedTool.Parent = plr.Backpack
		end
	end
	oldModel:Destroy()
end

local function sansWeakmode(plr, model)
	local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("Sans")
	local oldModel = plr.Character
	local newModel = model:Clone()
	newModel.Name = plr.Name
	plr.Character = newModel
	newModel.Parent = game.Workspace 
	newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
	for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
		local newObject = object:Clone()
		newObject.Parent = newModel
	end
	local tools = game.ReplicatedStorage.Tools.SansBitWeakTools:GetChildren()
	for _, tool in ipairs(tools) do
		local clonedTool = tool:Clone()
		clonedTool.Parent = plr.Backpack
	end
	oldModel:Destroy()
end

script.Parent.MouseClick:Connect(function(plr)
	rep.RemoteTools.Sans.OpenGui:FireClient(plr)
end)

rep.RemoteTools.Sans.Pacfistsans.OnServerEvent:Connect(function(plr)
	sansPacfistmode(plr)
end)

rep.RemoteTools.Sans.Weaksans.OnServerEvent:Connect(function(plr)
	local leaderstats = plr:FindFirstChild("leaderstats")
	if leaderstats then
		local data = mainstore:GetAsync(plr.UserId)
		if datastore["bitweaksans"] then
			print("You already own this character")
			sansWeakmode(plr)
		elseif leaderstats.Exp.Value == 100 or leaderstats.Exp.Value > 100 then
			print("Buying Character....")
			leaderstats.Exp.Value = leaderstats.Exp.Value - 100
			weakbitsans = true
			mainstore:SetAsync(plr, true)
			sansWeakmode(plr)
		else
			print("Haha your too broke")
		end
	end
end)

rep.RemoteTools.Sans.LastBreath.OnServerEvent:Connect(function(plr)
	LastBreath(plr)
end)

You try to get weakbitsans on the DataStoreService

Dis you read my first reply try to do that steps to get and load the data your data variable can you use to check the data you saved

You try to access this from the DataStoreService what won’t work maybe you try to acces the table What called savedata

1 Like

yea im trying so that uhh yea uhh yea

like this?

rep.RemoteTools.Sans.Weaksans.OnServerEvent:Connect(function(plr)
	local leaderstats = plr:FindFirstChild("leaderstats")
	if leaderstats then
		local success, data = pcall(mainstore.GetAsync, mainstore, plr.UserId)
		print("Buying Character....")
		if success then
			if data then
				weakbitsans = data.weakbitsans
			end
		end
		local data = mainstore:GetAsync(plr.UserId)
		if weakbitsans then
			print("You already own this character")
			sansWeakmode(plr)
		elseif leaderstats.Exp.Value == 100 or leaderstats.Exp.Value > 100 then
			print("BuyingCharacter..")
			leaderstats.Exp.Value = leaderstats.Exp.Value - 100
			weakbitsans = true
			local saveData = {
				weakbitsans = if weakbitsans then true else false,
			}
			mainstore:SetAsync(plr, true)
			local success, error = pcall(mainstore.SetAsync, mainstore, plr.UserId, saveData)
			if success then return end
			sansWeakmode(plr)
		else
			print("Haha your too broke")
		end
	end
end)

help, when i add another, it resets the other data, how to add multiple data at once?

local rep = game.ReplicatedStorage
local datastore = game:GetService("DataStoreService")
local mainstore = datastore:GetDataStore("MainDataStuff")
local weakbitsans = false
local lastbreathsans = false
local savedata = {
	
	["weakbitsans"] = false,
	["Lastbreathsans"] = false,
	["normalsans"] = false,
	["genocidesans"] = false,
	["vhssans"] = false,
	["cotv"] = false,
	["tears in the rain"] = false
}

mainstore:SetAsync(savedata)



local function sansPacfistmode(plr, model)
	local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("Sans")
	local oldModel = plr.Character
	local newModel = model:Clone()
	newModel.Name = plr.Name
	plr.Character = newModel
	newModel.Parent = game.Workspace 
	newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
	for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
		local newObject = object:Clone()
		newObject.Parent = newModel
	end
	local tools = game.ReplicatedStorage.Tools.SansWeakTools:GetChildren()
	for _, tool in ipairs(tools) do
		local clonedTool = tool:Clone()
		clonedTool.Parent = plr.Backpack
	end
	oldModel:Destroy()
end

local function LastBreath(plr, model)
	local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("LastBreathSans")
	local oldModel = plr.Character
	local newModel = model:Clone()
	newModel.Name = plr.Name
	plr.Character = newModel
	newModel.Parent = game.Workspace 
	newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
	for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
		local newObject = object:Clone()
		newObject.Parent = newModel
	end
	local tools = game.ReplicatedStorage.Tools.LastBreathTools:GetChildren()
	for _, tool in ipairs(tools) do
		if tool:IsA("Tool") then
			local clonedTool = tool:Clone()
			clonedTool.Parent = plr.Backpack
		end
	end
	oldModel:Destroy()
end

local function sansWeakmode(plr, model)
	local model = game.ReplicatedStorage.CharacterModel:FindFirstChild("Sans")
	local oldModel = plr.Character
	local newModel = model:Clone()
	newModel.Name = plr.Name
	plr.Character = newModel
	newModel.Parent = game.Workspace 
	newModel:SetPrimaryPartCFrame(game.workspace.SpawnLocation.CFrame) -- replace Spawn with the respawing part location
	for _, object in ipairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too.
		local newObject = object:Clone()
		newObject.Parent = newModel
	end
	local tools = game.ReplicatedStorage.Tools.SansBitWeakTools:GetChildren()
	for _, tool in ipairs(tools) do
		local clonedTool = tool:Clone()
		clonedTool.Parent = plr.Backpack
	end
	oldModel:Destroy()
end

script.Parent.MouseClick:Connect(function(plr)
	rep.RemoteTools.Sans.OpenGui:FireClient(plr)
end)

rep.RemoteTools.Sans.Pacfistsans.OnServerEvent:Connect(function(plr)
	sansPacfistmode(plr)
end)

rep.RemoteTools.Sans.Weaksans.OnServerEvent:Connect(function(plr)
	local leaderstats = plr:FindFirstChild("leaderstats")
	if leaderstats then
		local success, data = pcall(mainstore.GetAsync, mainstore, plr.UserId)
		print("Buying Character....")
		if success then
			if data then
				weakbitsans = data.weakbitsans
			end
		end
		local data = mainstore:GetAsync(plr.UserId)
		if weakbitsans then
			print("You already own this character")
			sansWeakmode(plr)
		elseif leaderstats.Exp.Value == 100 or leaderstats.Exp.Value > 100 then
			print("BuyingCharacter..")
			sansWeakmode(plr)
			leaderstats.Exp.Value = leaderstats.Exp.Value - 100
			weakbitsans = true
			local saveData = {
				lastbreathsans = if lastbreathsans then lastbreathsans else false,
				weakbitsans = if weakbitsans then true else false,
			}
			mainstore:UpdateAsync(plr, true)
			local success, error = pcall(mainstore.SetAsync, mainstore, plr.UserId, saveData)
			if success then return end
		else
			print("Haha your too broke")
		end
	end
end)

rep.RemoteTools.Sans.LastBreath.OnServerEvent:Connect(function(plr)
	local leaderstats = plr:FindFirstChild("leaderstats")
	if leaderstats then
		local success, data = pcall(mainstore.GetAsync, mainstore, plr.UserId)
		print("Buying Character....")
		if success then
			if data then
				lastbreathsans = data.lastbreathsans
			end
		end
		local data = mainstore:GetAsync(plr.UserId)
		if lastbreathsans then
			print("You already own this character")
			LastBreath(plr)
		elseif leaderstats.Exp.Value == 1200 or leaderstats.Exp.Value > 1200 then
			print("BuyingCharacter..")
			LastBreath(plr)
			leaderstats.Exp.Value = leaderstats.Exp.Value - 1200
			lastbreathsans = true
			local saveData = {
				lastbreathsans = if lastbreathsans then true else false,
				weakbitsans = if weakbitsans then weakbitsans else false,
			}
			mainstore:UpdateAsync(plr, true)
			local success, error = pcall(mainstore.SetAsync, mainstore, plr.UserId, saveData)
			if success then return end
		else
			print("Haha your too broke")
		end
	end
end)

You need to save the table then do

-- load 
local data = scope: getAsync (plr.userId) -- this the key we also use for setAsync

If data == nil then --we need to create the defould data first when there isn't
Scope:setAsync(plr.userId, savedate) -- for tables you only may use strings bools and numbers cuz it needs to match Json)
data = scope: getAsync(plr.userId) -- set the data to just saved valeu
end

Make sure that it will be Reset on adding new things you need to handle that but just make sure to make the table and not change it or add handling for new data but I’m busy with that to but it just errors rn I can help you with that when I fixed that

-- save 
Scope:setAsync(plr.userId, savedata) -- scope is the datastore variable mainstore

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.