I have been writing a script in Roblox that updates a players’ Money
value in a folder inserted in leaderstats
. Bascilly how everyone creates a leaderboard. The Money
value is changed by a ProximityPrompt``Triggered``Event
. It is first set off a data store script. I am very new to Data Store.
As I don’t know enough to try to fix this own my own nor able to read the API in the right context, anyhelp would be greatly appreciated. Here’s my code:
-- Money Update Script
local buttons = game.Workspace.buttons
-- Event Vars
-- Main Vars
local buy_railroad = buttons.buy_railroad.ProximityPrompt
local removeweeds1 = buttons.remove_weeds1.ProximityPrompt
local removeweeds2 = buttons.remove_weeds2.ProximityPrompt
-- Propertys
-- Action Vars
local weedremovemodel1 = workspace["weed remove1"]
local weedremovemodel2 = workspace["weed remove2"]
-- Misc Vars
local instructionsgui = buttons.buy_railroad.Part.BillboardGui.Frame
-- Logic
-- Functions
local function takemoneyfromplayers(player, money)
local playertotake = game.Players:FindFirstChild(player)
if playertotake then
local lstats = playertotake:FindFirstChild("leaderstats")
if lstats then
local moneystat = lstats:FindFirstChild("Money")
if moneystat and moneystat:IsA("IntValue") then
moneystat.Value = moneystat.Value - money
end
end
end
end
buy_railroad.Triggered:Connect(function(player)
instructionsgui.Visible = false
buy_railroad.Enabled = false
buy_railroad.Parent.Transparency = 1
removeweeds1.Enabled = true
removeweeds1.Parent.Transparency = 0.7
takemoneyfromplayers(player.Name,20000)
end)
removeweeds1.Triggered:Connect(function(player)
removeweeds1.Enabled = false
removeweeds1.Parent.Transparency = 1
removeweeds2.Enabled = true
removeweeds2.Parent.Transparency = 0.7
takemoneyfromplayers(player.Name,400)
for _, descendant in pairs(weedremovemodel1:GetDescendants()) do
if descendant:IsA("BasePart") or descendant:IsA("MeshPart") then
descendant.CanCollide = false
for transparency = 0.2, 1, 0.1 do
if transparency < 1 then
descendant.Transparency = transparency
task.wait(0.01)
else
break
end
end
end
end
end)
removeweeds2.Triggered:Connect(function(player)
removeweeds2.Enabled = false
removeweeds2.Parent.Transparency = 1
takemoneyfromplayers(player.Name,400)
for _, descendant in pairs(weedremovemodel2:GetDescendants()) do
if descendant:IsA("BasePart") or descendant:IsA("MeshPart") then
descendant.CanCollide = false
for transparency = 0.2, 1, 0.1 do
if transparency < 1 then
descendant.Transparency = transparency
task.wait(0.01)
else
break
end
end
end
end
end)
--Data Store Script
local DSS=game:GetService("DataStoreService")
local mainDS=DSS:GetDataStore("Money")
local ReplicatedStorage=game:GetService("ReplicatedStorage")
local Events=ReplicatedStorage:FindFirstChild("Events")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats=Instance.new("Folder",player)
leaderstats.Name="leaderstats"
local money=Instance.new("IntValue",leaderstats)
money.Name="Money"
local data=mainDS:GetAsync(player.UserId)
if data then
if data[1] then
money.Value=data[1]
else
money.Value=20000
end
end
money.Changed:Connect(function()
mainDS:SetAsync(player.UserId, {money.Value})
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local leaderstats=player:WaitForChild("leaderstats")
local money=leaderstats:WaitForChild("Money")
mainDS:SetAsync(player.UserId,{money.Value})
end)
I expect the data to save once i exit the game, but it doesn’t. It appeers to on the leaderboard, but after in the money script when Itake money away, it defults to the data store. I am sure of this due to printing the data store with the User’s ID and the correct key, "Money"
and then printing it with a seperate script. It prints a dictionary with an empty Instance
.