Before I start, I want to say that this is my first tutorial and I’m not really good at introducing myself and my topic
Skip to the end for full script.
Hello developers of Roblox. So I was browsing through the forum, and I saw someone wanted to make a gifting system. So that gave me the idea to create tutorial on how to make one.
First, you want to make a DataStore, to save the player’s stuff.
local DataStoreService = game:GetService('DataStoreService')
local CoinsDataStore = DataStoreService:GetDataStore('CoinsDataStore')
Now I have the DataStores set up. Next we have to make some type of currency to be given.
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new('Folder', player)
leaderstats.Name = 'leaderstats' -- make sure to always name it 'leaderstats'
local Coins = Instance.new('IntValue', leaderstats)
Coins.Name = 'Coins'
player.CharacterAdded:Connect(function(char)
-- this part isn't needed for this, i just like to add it
end)
end)
Now we have the leaderstats made. Now we have to attempt to load it.
local CoinsData
local success, errormessage = pcall(function()
CoinsData = CoinsDataStore:GetAsync('Coins_'..player.UserId)
end)
Make sure to always set the data to the player’s UserId, because the data will not save if the player changes their name.
Now we can set it’s coins to the data we get from the GetAsync function
if CoinsData and success then
player.leaderstats.Coins.Value = CoinsData
else -- if it is returning nil
warn(errormessage)
end
Alright, now we have the main part set up.
The script will look like this:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new('Folder', player)
leaderstats.Name = 'leaderstats' -- make sure to always name it 'leaderstats'
local Coins = Instance.new('IntValue', leaderstats)
Coins.Name = 'Coins'
player.CharacterAdded:Connect(function(char)
-- this part isn't needed for this, i just like to add it
end)
local CoinsData
local success, errormessage = pcall(function()
CoinsData = CoinsDataStore:GetAsync('Coins_'..player.UserId)
end)
if CoinsData and success then
player.leaderstats.Coins.Value = CoinsData
else -- if it is returning nil
warn(errormessage)
end
end)
Now we will save the data when the player leaves
game.PlayersRemoving:Connect(function(player)
local Coins = player.leaderstats.Coins
local DataSaving
local success, errormessage = pcall(function()
CoinsDataStore:SetAsync('Coins_'player.UserId, Coins)
end)
end)
And lastly, make sure it saves the data (shoutout to @JackscarIitt because I learned this from him).
game:BindToClose(function()
for i, v in pairs(game.Players:GetChildren()) do
CoinsDataStore:SetAsync('Coins_'v.UserId, v.leaderstats.Coins)
end
end)
Ok well that was the first part of this script.
Will look like this:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new('Folder', player)
leaderstats.Name = 'leaderstats' -- make sure to always name it 'leaderstats'
local Coins = Instance.new('IntValue', leaderstats)
Coins.Name = 'Coins'
player.CharacterAdded:Connect(function(char)
-- this part isn't needed for this, i just like to add it
end)
local CoinsData
local success, errormessage = pcall(function()
CoinsData = CoinsDataStore:GetAsync('Coins_'..player.UserId)
end)
if CoinsData and success then
player.leaderstats.Coins.Value = CoinsData
else -- if it is returning nil
warn(errormessage)
end
end)
game.PlayersRemoving:Connect(function(player)
local Coins = player.leaderstats.Coins
local DataSaving
local success, errormessage = pcall(function()
CoinsDataStore:SetAsync('Coins_'player.UserId, Coins)
end)
end)
game:BindToClose(function()
for i, v in pairs(game.Players:GetChildren()) do
CoinsDataStore:SetAsync('Coins_'v.UserId, v.leaderstats.Coins)
end
end)
Now for part 2: Gifting.
For this, I will use 2 text boxes, one for the amount, and one for the player
Make a remote event, call it Gift, place it in Replicated storage.
local amountTextBox = -- define the text box that you want as the amount
local chosenPlayer = -- define this too
local amount
local PlayerId
amountTextBox.FocusLost:Connect(function(entered)
if entered then
amount = tonumber(amountTextBox.Text)
if amount and PlayerId then
game.ReplicatedStorage.Gift:FireServer(amount, PlayerId)
end
end
end)
chosenPlayer.FocusLost:Connect(function(entered)
if entered then
if game.Players:GetUserIdFromNameAsync(chosenPlayer.Text) then
PlayerId = game.Players:GetUserIdFromNameAsync(chosenPlayer.Text)
if amount and PlayerId then
game.ReplicatedStorage.Gift:FireServer(amount, PlayerId)
end
end
end
end)
There we made it so that it will tell the client to update the chosen player’s Coins. One box will have the amount, and one will have the Player’s Id.
Now we can make a function for the server to tell when the event was fired.
game.ReplicatedStorage.Gift.OnServerEvent:Connect(function(player, amount, Id)
local coins
local success, err = pcall(function()
coins = CoinsDataStore:GetAsync('Coins_'..Id)
end)
if success then
local success2, err2 = pcall(function()
CoinsDataStore:SetAsync('Coins_'..Id, coins + amount)
end)
if success2 then; print('Saved'); end
else
warn('error')
end)
(If I made some errors, please correct me, I couldn’t test it.)
Well, that should be it. This is the full script:
Server script:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new('Folder', player)
leaderstats.Name = 'leaderstats' -- make sure to always name it 'leaderstats'
local Coins = Instance.new('IntValue', leaderstats)
Coins.Name = 'Coins'
player.CharacterAdded:Connect(function(char)
-- this part isn't needed for this, i just like to add it
end)
local CoinsData
local success, errormessage = pcall(function()
CoinsData = CoinsDataStore:GetAsync('Coins_'..player.UserId)
end)
if CoinsData and success then
player.leaderstats.Coins.Value = CoinsData
else -- if it is returning nil
warn(errormessage)
end
end)
game.PlayersRemoving:Connect(function(player)
local Coins = player.leaderstats.Coins
local DataSaving
local success, errormessage = pcall(function()
CoinsDataStore:SetAsync('Coins_'player.UserId, Coins)
end)
end)
game:BindToClose(function()
for i, v in pairs(game.Players:GetChildren()) do
CoinsDataStore:SetAsync('Coins_'v.UserId, v.leaderstats.Coins)
end
end)
game.ReplicatedStorage.Gift.OnServerEvent:Connect(function(player, amount, Id)
local coins
local success, err = pcall(function()
coins = CoinsDataStore:GetAsync('Coins_'..Id)
end)
if success then
local success2, err2 = pcall(function()
CoinsDataStore:SetAsync('Coins_'..Id, coins + amount)
end)
if success2 then; print('Saved'); end
else
warn('error')
end)
Client (local script):
local amountTextBox = -- define the text box that you want as the amount
local chosenPlayer = -- define this too
local amount
local PlayerId
amountTextBox.FocusLost:Connect(function(entered)
if entered then
amount = tonumber(amountTextBox.Text)
if amount and PlayerId then
game.ReplicatedStorage.Gift:FireServer(amount, PlayerId)
end
end
end)
chosenPlayer.FocusLost:Connect(function(entered)
if entered then
if game.Players:GetUserIdFromNameAsync(chosenPlayer.Text) then
PlayerId = game.Players:GetUserIdFromNameAsync(chosenPlayer.Text)
if amount and PlayerId then
game.ReplicatedStorage.Gift:FireServer(amount, PlayerId)
end
end
end
end)