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

who can help me with my bank data store saving?

I have a bank system and i want to make transfers to offline people. but there is something wrong. what have i done. i made a database so when people come only it saved there name and id. when i want to transfer to the people that have played it calls that database so i can have an id from that player. so i get a list from names and ids and can choose from that. it save the date as value in the game so i can access it with other scripts. in a other script i say get the value and make a name for the database with player-id and a name behind it like bankcoins so u get as id -> playeruserid-bankcoins <- the i set that with async so its saved with the value i want to give from my bank.

then when the player comes online it check the ->playeruserid-bankcoins<- database if there is some in it. when something in it add it to his original bank coins.

now the problem. when i transfer to some one else (so not me) and i go offline and i come back online. it give me back what i give the other player. why its not even save on my id number.

here are the codes:

script in serverscriptservice

local DataStore2 = require(1936396537) -- <-- Data store

local DataStoreName = game.ReplicatedStorage.Values.ServerName.Value

local DataStoreService = game:GetService("DataStoreService") -- <-- Data store


local ServerData = DataStoreService:GetDataStore(DataStoreName) -- <-- Data store


game.Players.PlayerAdded:Connect(function(player)

	local BankCoinsDataStore = DataStore2("BankCoins",player) -- <-- Data store


	local NameData = DataStoreService:GetDataStore("PlayerNameData") -- <-- Data store

	local IdData = DataStoreService:GetDataStore("PlayerIdData") -- <-- Data store


	local spelerscount = game.ReplicatedStorage.Values.SpelersCount

	local ServerId = game.ReplicatedStorage.Values.ServerId.Value

	local i = spelerscount.Value
	local newi = spelerscount.Value + 1
	local found = false

	-- check the players i have

	for i = spelerscount.Value, 0, -1 do
		local pname = NameData:GetAsync(i)
		local pid = IdData:GetAsync(i)
		if pid == player.UserId and pname == player.Name then
			found = true
			print("Old Player")
		end
	end

	-- if not exists then add it
	if found == false then
		NameData:SetAsync(newi, player.Name)
		IdData:SetAsync(newi, player.UserId)
		spelerscount.Value = newi
		local DataStoreName = game.ReplicatedStorage.Values.ServerName.Value
		local ServerDataStore = game:GetService("DataStoreService")
		--	Get the data from DataStoreName
		local ServerData = ServerDataStore:GetDataStore(DataStoreName)
		local spelercountdata = (ServerId.."-"..spelerscount.Name)
		ServerData:SetAsync(spelercountdata,spelerscount.Value)
	
		print("New Player")
	end

	-- the bank part im talking about

	-- make the name
	local OTCV = {player.UserId.."-OTCV"}
	local OTGV = {player.UserId.."-OTGV"}

	--get the database

	local OTCGC = ServerData:GetAsync(OTCV) -- <-- Data store

	wait(7)
	local OTGGG = ServerData:GetAsync(OTGV) -- <-- Data store

	wait(7)

	--if its nothing make it 0

	if OTCGC == false or nil then 
		ServerData:SetAsync(OTCV,0) -- <-- Data store

		OTCGC = 0
	end
	wait(7)
	if OTGGG == false or nil then 
		ServerData:SetAsync(OTGV,0) -- <-- Data store

		OTGGG = 0
	end
	wait(7)

	-- if there is some thing add to bank account and save that there is nothing to get any more (so make it 0) that if he comes online again he cant get the same amount it was

	if OTCGC > 0 then
	
		BankCoinsDataStore:Increment(OTCGC,defaultCoinsValue) -- <-- Data store

		ServerData:SetAsync(OTCV,0)-- <-- Data store

	end
	wait(7)
	if OTGGG > 0 then
		
		BankGoldDataStore:Increment(OTGGG,defaultGoldValue)-- <-- Data store

		ServerData:SetAsync(OTGV,0)-- <-- Data store

	end
	
end)

script from my gui screen to get the names

local DataStore2 = require(1936396537)-- <-- Data store


local DataStoreService = game:GetService("DataStoreService")-- <-- Data store

local Players = game:GetService("Players"):GetPlayers()
local map = script.Parent
local Frame = map.Frame
local Ui = map.ScrollingFrame
local player = map.Parent.Parent

wait(1)

local NameData = DataStoreService:GetDataStore("PlayerNameData")
local IdData = DataStoreService:GetDataStore("PlayerIdData")

local spelerscount = game.ReplicatedStorage.Values.SpelersCount
	
local i = spelerscount.Value

		
if i ~= 0 then
	for i = spelerscount.Value, 0, -1 do
		local pname = NameData:GetAsync(i)-- <-- Data store

		local pid = IdData:GetAsync(i)-- <-- Data store

		if pid ~= player.UserId and pname ~= player.Name and pid ~= nil and pname ~= nil then
					
			local copy = Frame:Clone()
			copy.AName.Value = pname
			copy.AID.Value = pid
			copy.Visible = true
			copy.a.Text = pname
			copy.aid.Text = pid
			copy.a.Name = pname
			copy.aid.Name = pid
			copy.Name = pname
			copy.Ja.Disabled = false
	
			copy.Parent = Ui
			
		end
	end
end

next script when i chose that player

wait(1)

local button = script.Parent.BName

local Give = game.ReplicatedStorage.Gui.OTGFrame
local Frame = script.Parent
local Gui = Frame.Parent.Parent
local p = Gui.Parent.Parent
Frame.BBName.Value = p.Name


button.MouseButton1Down:connect(function()

	local copy = Give:Clone()
	copy.Parent = Gui
	copy.AName.Value = Frame.AName.Value
	Frame.Visible = false
	Frame.Parent = copy
	
	local SF = Gui.ScrollingFrame
	
	SF:Destroy() 
	
end)

and then the next script to set amount and save it with async so the player can get it when he come online

local DataStore2 = require(1936396537)-- <-- Data store

local DataStoreName = game.ReplicatedStorage.Values.ServerName.Value

--	Get service
local ServerDataStore = game:GetService("DataStoreService")-- <-- Data store

--	Get the data from DataStoreName
local ServerData = ServerDataStore:GetDataStore(DataStoreName)-- <-- Data store


local Players = game:GetService("Players"):GetPlayers()

local defaultCoinsValue = 0

wait(1)

local Okebutton = script.Parent.OkeButton
local DownButton = script.Parent.DownButton
local Down10Button = script.Parent.Down10Button
local UpButton = script.Parent.UpButton
local Up10Button = script.Parent.Up10Button
local close = script.Parent.CloseButton

local GaveBankCoinsDataStore

local GiveNamea = script.Parent.AName
local GiveNameb = script.Parent.AName.Value
local GiveIda = script.Parent[GiveNameb].AID
local GaveNamea = script.Parent[GiveNameb].BBName

for i, player in pairs(Players) do
	
	if player.Name == GaveNamea.Value then
		
		GaveBankCoinsDataStore = DataStore2("BankGold",player)-- <-- Data store

		
	end
end


local GaveAmount = GaveBankCoinsDataStore:Get(defaultCoinsValue)-- <-- Data store


local BValue = script.Parent[GiveNameb].AValue
local CValue = GaveAmount

script.Parent.AmountLabel.Text = BValue.Value
script.Parent.AAmountLabel.Text = CValue
local oldValue

local OTCVData = {GiveIda.Value.."-OTGV"}-- <-- Data store

name or id from player to give to

UpButton.MouseButton1Down:Connect(function(p)
	
	BValue.Value = BValue.Value + 1
	script.Parent.AmountLabel.Text = BValue.Value
	
end)

Up10Button.MouseButton1Down:Connect(function(p)
	
	BValue.Value = BValue.Value + 10
	script.Parent.AmountLabel.Text = BValue.Value
	
end)

DownButton.MouseButton1Down:Connect(function(p)
	
	if BValue.Value > 0 then
		
		BValue.Value = BValue.Value - 1
		script.Parent.AmountLabel.Text = BValue.Value
		
	end
	
end)

Down10Button.MouseButton1Down:Connect(function(p)
	
	if BValue.Value > 9 then
		
		BValue.Value = BValue.Value - 10
		script.Parent.AmountLabel.Text = BValue.Value
		
	end
	
end)

close.MouseButton1Down:Connect(function()
	
	local Map = script.Parent.Parent
	local Screen = Map.Parent.Screen
		
		Screen.Value = false
 
    Map:Destroy()
	
end)

Okebutton.MouseButton1Down:connect(function(p)
	
	if CValue >= BValue.Value then
 
		GaveBankCoinsDataStore:Increment(-BValue.Value,defaultCoinsValue)-- <-- Data store

		oldValue = ServerData:GetAsync(OTCVData)-- <-- Data store

		
		if oldValue == nil or oldValue == false then
			oldValue = 0
		end
		
		local newValue = oldValue + BValue.Value
		ServerData:SetAsync(OTCVData, newValue)-- <-- Data store

		
		script.Parent.OkeButton.Text = "Done"
		
		wait(3)
		
		local Map = script.Parent.Parent
		local Screen = Map.Parent.Screen
		
		Screen.Value = false
 
	    Map:Destroy()

	else
		
		script.Parent.OkeButton.Text = "Not enuf"
		
	end
	
	
end)

so what do u think? what did i wrong? why do i get the amount back i gave away while i saved it on an other id?

thanks for the help already

6 comments

0

Personally, I prefer to use datastore. I find it to be way easier and easy to access.With the normal datastore, I could give you a code example in >50 lines

0

i use datastore already for ingame. but cant use datastore with only a playerid, u get an error, so i have to make the save my self with a set and get async.

0

mc3334 read the script first befor u command something then u saw i used already database

0

not true^

0

Im writing a solution rn, just give me a couple more mins to finish it up.

0

ok, I just wrote what I think is my longest answer ever

2 answers

✓ Accepted answer

To transfer to offline people sounds like a cool and unique feature. To do this, you will have a lot of data. Obviously, you cant make data for every person on roblox and do it like that, so I suggest something like this.

SETUP: 1)Make a screen GUI under StarterGui

-Name it "Money Transfer"

2)Make a frame under "Money Transfer"

3)Make a text box under The frame from step 2

-Name it "plrName"

-PlaceholderText = "Put the player's name here"

4)Make a text box under the frame from step 2

-Name it "ID"

-PlaceholderText = "Put the player's ID here"

5)Make a text box under the frame from step 2

-Name it "Amount"

-PlaceholderText = "Amount"

6)Make a text button under the frame from step 2

-Name it "Done"

-Set the text to "Transfer"

7)Make a local script under the GUI from step 1

-Name it "LocalTransfer"

8)Make a server script under ServerScriptService

-Name it "Money Main"

9)Make a remote event under replicated storage

-Name it "TransferMoney"

CODING LOCAL SCRIPT FROM STEP 7:

transfer = game:GetService("ReplicatedStorage"):WaitForChild("TransferMoney")
f = script.Parent:WaitForChild("Frame")
done = f:WaitForChild("Done")

done.MouseButton1Click:Connect(function()
	if f.plrName.Text ~= "" and f.ID.Text ~= "" and f.Amount.Text ~= "" then
		transfer:FireServer(f.plrName.Text..f.ID.Text,f.Amount.Text)
		f.plrName.Text = ""
		f.ID.Text = ""
		f.Amount.Text = ""
	end
end)

SERVER SCRIPT FROM STEP 8:

startingMoney = --Money a player will start with | EX: 100

DS = game:GetService("DataStoreService"):GetDataStore("Money")
ev = game:GetService("ReplicatedStorage"):WaitForChild("TransferMoney")

game.Players.PlayerAdded:Connect(function(plr)
	local data = DS:GetAsync(plr.Name..plr.UserId)
	if not data then
		data = startingMoney
	end
	local ls = Instance.new("Folder")
	ls.Parent = plr
	ls.Name = "leaderstats"
	local Cash = Instance.new("INTvalue")
	Cash.Parent = ls
	Cash.Name = "Money"
	Cash.Value = data
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local data = plr.leaderstats.Money.Value
	pcall(function()
		DS:SetAsync(plr.Name..plr.UserId,data)
	end)
end)

ev.onServerEvent:Connect(function(plrfrom,plrto,amount)
	if tonumber(amount) == nil then return end
	if plrfrom.leaderstats.Money.Value < amount then return end
	local data = DS:GetAsync(plrto)
	if not data then
		data = startingMoney
	end
	local newData = data+amount
	plr.leaderstats.Money.Value = plr.leaderstats.Money.Value - amount
	DS:SetAsync(plrto,newData)
end

CONTEXT In the server script, the player's data loads and saves when they join and leave. It also receives the request to transfer money from the local script. In the local script, we simply send the request to transfer money. ALSO: as a response to why your DS doesn't work when you try to save with playerID, datastores only accept text, so you would have to do "tostring(ID)" to save it properly. I hope my response was able to help you. (I kinda did wing it and its a nice chunk of code so if there are any bugs you can't figure out, just respond to my answer). Happy scripting! ~mc3334

5 comments

0

and already have a gui and stuff, read script better

0

and i only added the part that are needed here else u get a script from 5000 lines

0

tbfh, I didnt read your 150 line script im sorry...... I proposed a better and quicker solution in 50 lines.

0

little erro at cash.parent. = not plr but leaderstats

0

Thanks, I fixed it

this dose not do what i want. it dont save the transfer amount i want to give the offline player!!!! if no one is in the server the server shuts down and the transfer u made will be gone too. as i sayed i already have a datastore but i need help with the datastore to give offline players money. datastore2 is required in my script for saving the online players stuff. but how do i stansfer some money to offline player that if they come online they get the amount others sended. and not that other get what they dont deserve. i send money to a friend that is offline and when i go off and on i have the amount back. how??? its not even saved under my id but saved under the id of the friend

10 comments

0

Not true and not true. This does what you intend, your just reading it incorrectly. AND I am setting it to the ID, as I use the key format of "name..id". AND it will save across servers because its called saving it to a datastore and loading it. please figure out what your talking about before you come at my code.

0

PLUS as seen in line 25, your money will be deducted and then saved when you leave, your probably just not using it right

0

PLUS as seen in line 25, your money will be deducted and then saved when you leave, your probably just not using it right

0

and where is the save for the amount i give the offline players????? no datastore for that. u only save the value the player already has not the givenamount. so when i put down the server the givenamount is gone u didnt save it

0

oh i see, u used fireserver for that some how, but i dont know what that is. but as u see i already have some like that only it ready or wriths it wrong some how. can u tell me what the fireserver does

0

the problem is that i have already a database and player stats and every thing ready. but when i say save it on (playertogiveid..aname) it sould giver that player the amout i set behind it, but what happens when i enter the game it look under my id and i get the amount the i set for the playertogive id back. while its not saved on my id. how is this possible????

0

i get a name and id from database i made so i have his id. i save the value in a intvalue and ask that back in other script, then i make a name with his id.."-bankcoinstransfer" then i say setasync(id.."bankcoinstransfer", amount) _<saved his id. on load it look on (id.."bankcoinstransfer") if there is something but it looks my id then so i should not get the amount but still he give it to me

0

just. use. my. solution. Datastore 2 is way longer and just unnecessary in this case

0

And as you see in line 36, it does save to the datastore. I dont know what your saying about a seperate data store or something. Its hard to understand because your grammar isnt the best.

0

true im not the best ind i tryed ur solution and it works fine. thanks. i only try to understand it ann and im new to scripting, but im good at it i notice. only some times have to figure some stuff out. like this datastore.

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