Hello! I have been trying to create a cleaning system where once a ProximityPrompt is activated, the player is then awarded a point and the point is saved in a DataStore, however I’m currently having issues as it doesn’t seem to be giving me a point when I activate it. If anyone knows why this may be, or spots another mistake with my code that may interferre with things, please let me know!
local DataStoreService = game:GetService("DataStoreService")
local PointsDataStore = DataStoreService:GetOrderedDataStore("APS_Points_1221")
script.Parent.Triggered:Connect(function(Player)
script.Parent.Parent.Transparency = 1
script.Parent.Enabled = false
local function awardPoints(player: Player, points: number)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
leaderstats.Points.Value = leaderstats.Points.Value + 1
end
task.defer(function()
local success, err = pcall(function()
PointsDataStore:IncrementAsync(player.UserId, points)
end)
if not success then
warn("Error awarding points:", err)
end
end)
end
wait (100)
script.Parent.Parent.Transparency = 0.5
script.Parent.Enabled = true
end)
Any help people have to offer is much appreciated, thanks!
When the proximity prompt is triggered you are creating a function called awardPoints but at no point are you actually calling the function to award the points:
local DataStoreService = game:GetService("DataStoreService")
local PointsDataStore = DataStoreService:GetOrderedDataStore("APS_Points_1221")
local function awardPoints(player: Player, points: number)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
leaderstats.Points.Value = leaderstats.Points.Value + 1
end
task.defer(function()
local success, err = pcall(function()
PointsDataStore:IncrementAsync(player.UserId, points)
end)
if not success then
warn("Error awarding points:", err)
end
end)
end
script.Parent.Triggered:Connect(function(Player)
script.Parent.Parent.Transparency = 1
script.Parent.Enabled = false
awardPoints(Player, 1)
wait (100)
script.Parent.Parent.Transparency = 0.5
script.Parent.Enabled = true
end)
I wouldn’t recommend saving to datastore every time the player earns a point. I don’t believe this is the source of this problem, but it can become a problem of its own as datastores get overwhelmed if you write to them too frequently. An alternative is to have saving be done as the player leaves the game, when the player presses a button to save, and at random autosave intervals.