Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

DataStore2 won't save value after rewarding player with points?

I'm trying to create a script that rewards the player 100 points for killing other players. The script works but since I'm using DataStore2 you're supposed to update their saved data instead of updating their leaderstats. (Or only the leaderstat will update but it won't be saved) I don't understand how to do this though because this is my first time trying to use DataStore2

Code:

local DataStore2 = require(game.ServerScriptService.StatsDataStore2:WaitForChild("MainModule"))
local Humanoid = script.Parent.Humanoid
function dead() 
local tag = Humanoid:findFirstChild("creator") 
	if tag ~= nil then 
		if tag ~= nil then 
local Leaderstats = tag.Value:findFirstChild("PlayerStats") 
		if Leaderstats ~= nil then 
        Leaderstats.Points.Value = Leaderstats.Points.Value + 100
	--[[local PointsStore = DataStore2("Points", tag)
		PointsStore:Increment(100) <-- My attempt at trying to update the value of their saved data didn't work  ]]
wait(0.1)
script:Destroy()
			end 
		end 
	end 
end 
Humanoid.Died:connect(dead) 

Extra Info: This script is placed in StarterCharacter (Not sure if that's relevant or not)

1 answer

I'm kinda inexperienced in DataStore2, but I think I can help. To me, it looks like you forgot to set the updated value.

So you could do the following:

local PointsStore = DataStore2("Points", tag):Get() -- Gets value
        PointsStore:Increment(100)  -- Increments value
PointStore:Set(PointsStore) -- Sets the updated value

Hopefully I was right!

This discussion is preserved as it was. New answers, comments, and votes are closed.