In todays video I show you how to make a Part or a Item give you currency (Cash or Coins) when the item is touched, this script includes a data store, meaning if a player leaves the game the data saves and when they rejoin they will have the same amount of currency before they left. I Hope you enjoyed the video, Have a lovely rest of your day!
If you are a bit confused what to do, Feel free to message me on Discord and Ill help you out!
🌟 Floppys Discord Server: [ Ссылка ]
Script 1 (Leaderstats Script) :
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coin = Instance.new("IntValue")
Coin.Name = "Coins" -- Change this to what you want the leaderstat name to be
Coin.Value = 0
Coin.Parent = leaderstats
end)
Script 2 (Coin Script) :
local DataStoreService = game:GetService("DataStoreService")
local CoinsDataStore = DataStoreService:GetDataStore("CoinsDataStore")
local RespawnTime = 5 -- Change the number to adjust how often you want the coin to respawn
function savePlayerData(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local coins = leaderstats:FindFirstChild("Coins")
if coins then
CoinsDataStore:SetAsync(player.UserId .. "_Coins", coins.Value)
end
end
end
function loadPlayerData(player)
local success, result = pcall(function()
return CoinsDataStore:GetAsync(player.UserId .. "_Coins")
end)
if success and result ~= nil then
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local coins = leaderstats:FindFirstChild("Coins")
if coins then
coins.Value = result
end
end
end
end
function onTouch(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then
return
end
local coins = leaderstats:FindFirstChild("Coins")
if coins then
coins.Value = coins.Value + 1 -- Change the +1 to how much currency you want that coin to give
end
script.Parent.CanTouch = false
script.Parent.Transparency = 1
wait(RespawnTime)
script.Parent.CanTouch = true
script.Parent.Transparency = 0
end
end
function onPlayerRemoving(player)
savePlayerData(player)
end
function onPlayerAdded(player)
loadPlayerData(player)
end
script.Parent.Touched:Connect(onTouch)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)
game.Players.PlayerAdded:Connect(onPlayerAdded)
🌟 Floppys Roblox Game : [ Ссылка ]...
Roblox Studio Tutorial
Roblox Studio
Studio Tutorials
![](https://i.ytimg.com/vi/H6LsbS_SA2w/maxresdefault.jpg)