Hi guys, so I put two new scripts into my game today. One responds to a remote event in repstore, and the other runs on startup. Here’re the contents:
Leaderstats Script:
–bigcrazycarboy
local DataStore = game:GetService("DataStoreService")
local BankStore = DataStore:GetDataStore("SunFirstBankCoralCity")
local PocketStore = DataStore:GetDataStore("PlayerPocketCash")
game.Players.PlayerAdded:connect(function(newPlayer)
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Name = "Cash"
if PocketStore:GetAsync(newPlayer.UserId) then
cash.Value = PocketStore:GetAsync(newPlayer.UserId)
else
cash.Value = 500
PocketStore:SetAsync(newPlayer.UserId, cash.Value)
end
cash.Parent = stats
stats.Parent = newPlayer
if BankStore:GetAsync(newPlayer.UserId) then
return
else
BankStore:SetAsync(newPlayer.UserId, 0)
end
while true do
wait(200)
if newPlayer.TeamColor ~= BrickColor.new("Fossil") then
cash.Value = cash.Value + 30
end
end
end)
game.Players.PlayerRemoving:connect(function(player)
local cash = player:WaitForChild("leaderstats").Cash
PocketStore:SetAsync(player.UserId, cash.Value)
print("Saving Data for "..player.Name.." before they leave.")
end)
The next one is the event responder.
–bigcrazycarboy
local DataStore = game:GetService("DataStoreService")
local BankStore = DataStore:GetDataStore("SunFirstBankCoralCity")
local PocketStore = DataStore:GetDataStore("PlayerPocketCash")
game.ReplicatedStorage.RemoteEvents.BankMoney.OnServerEvent:connect(function(player, action, amount)
if action == "Deposit" then
local Money = BankStore:GetAsync(player.UserId)
local NewMoney = Money + math.abs(amount)
BankStore:SetAsync(player.UserId, NewMoney)
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - math.abs(amount)
print("Deposited "..(math.abs(amount)).." into "..player.Name.."'s bank account.")
elseif action == "Withdraw" then
local Money = BankStore:GetAsync(player.UserId)
local NewMoney = Money - math.abs(amount)
if NewMoney > 0 then
BankStore:SetAsync(player.UserId, NewMoney)
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + math.abs(amount)
print("Withdrew "..(math.abs(amount)).." into "..player.Name.."'s bank account.")
end
end
end)
--[[
game.Players.PlayerAdded:connect(function(newPlayer)
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = PocketStore:GetAsync(newPlayer.UserId) or 500
cash.Parent = stats
stats.Parent = newPlayer
while true do
wait(200)
if newPlayer.TeamColor ~= BrickColor.new("Fossil") then
cash.Value = cash.Value + 30
end
end
end)
game.Players.PlayerRemoving:connect(function(player)
local c = player.leaderstats.Cash
PocketStore:SetAsync(player.UserId, c)
print("Saving Data for "..player.Name.." before they leave.")
end)]]
For some reson this keeps on clearing everything in workspace. I have StudioAPI enabled. Hopefully someone can help, and excuse any grammar issues I’m tired. What am I doing wrong? Thanks!
-big