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

How To Make It Save The Cost Of An Item And Save IntValues?

https://cdn.discordapp.com/attachments/447457287376994304/494905176889753610/unknown.png this is the folders

this is the script for the shop

button = game.StarterGui.Shop.Frame.ImageButton
player = game.Players.LocalPlayer
cost = game.ReplicatedStorage.StatFile.LocalPlayer.statnumbers.cost1.Value
add = 1

button.MouseButton1Down:connect(function()
	if player.leaderstats.Money.Value >= cost then
		player.leaderstats.Money.Value = player.leaderstats.Money.Value - cost
		player.leaderstats.MPS.Value = player.leaderstats.MPS.Value + add
		cost = cost + 50
	end
end)


while true do
	wait(0.01)
	script.Parent.Text = "Cost: "..cost
end

also i want it to save the cost the intvalue but im not sure if i have done the datastore correctly

local DS = game:GetService("DataStoreService"):GetDataStore("test")
local StatFile = game:GetService("ReplicatedStorage"):WaitForChild("StatFile")

game.Players.PlayerAdded:connect(function(player)
	local playerFolder = Instance.new("Folder", StatFile)
	playerFolder.Name = player.Name
	
	local statnumbers = Instance.new("Folder", playerFolder)
	statnumbers.Name = "statnumbers"
	
	local cost1 = Instance.new("IntValue", statnumbers)
	cost1.Name = "cost1"
	
	local add1 = Instance.new("IntValue", statnumbers)
	add1.Name = "add1"
	
	local thing = Instance.new("StringValue", statnumbers)
	thing.Name = "thing"
	
	local str = Instance.new("StringValue", statnumbers)
	str.Name = 'str'
	str.Value = 'thisIsAString'
	
	local savedStuff = DS:GetAsync(player.userId)
	
	if savedStuff then
		print('data takes up '..game:GetService("HttpService"):JSONEncode(savedStuff):len()..'/260000')
		for i,v in pairs(savedStuff)do
			print(i,v)
			if i % 2 == 0 then
				StatFile[player.Name].statnumbers:FindFirstChild(savedStuff[i - 1]).Value = v
				
			end
		end
	end
end)

local function saveValues(player)
	local savedData = {}
	for i,v in pairs(StatFile[player.Name].statnumbers:GetChildren()) do 
		savedData[#savedData + 1] = v.Name
		savedData[#savedData + 1] = v.Value
	end
	DS:SetAsync(player.userId, savedData)
end

game:BlindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		saveValues(player)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	saveValues(player)
end)

1 answer

try this :)

local ds = game:GetService("DataStoreService"):GetDataStore("Pillows")
local tbl = {}
local array = 0

game.Players.PlayerAdded:Connect(function(p)
	player = p
	local data = ds:GetAsync(p.UserId)
	local buttons = p.PlayerGui:WaitForChild("Shop").ShopFrame.ShopGui.Buttons
	if data == nil then
		print("Creating new data")
		ds:SetAsync(p.UserId, tbl)
		else
		for _, v in pairs(buttons:GetChildren())do
			if v.ClassName == "TextButton" then
				array = array + 1
				v.Amount.Value = data[array]
			end
		end
	end
end)

game:BindToClose (function()
	local ID = player.UserId
	local buttons = player.PlayerGui:WaitForChild("Shop").ShopFrame.ShopGui.Buttons
	for _, v in pairs(buttons:GetChildren())do
			if v.ClassName == "TextButton" then 
				table.insert(tbl, v.Amount.Value)
			end
	end
	ds:SetAsync(ID, tbl)
end)

3 comments

0

do i put that in the shop item or the datastore?

0

script on Workspace (it on my game so insert int values on Buttons for buying can name them ammount and then fix the path) it works fine on me

0

i dont really understand cause im quite new to scripting is there anyway you could try fix it its very confusing to me

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