Whats Wrong With This DataStore Script?

Whats wrong with this script ?

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“MyDataStore”)

game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = plr

local points = Instance.new("NumberValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats

local wins = Instance.new("NumberValue")
wins.Name = "Wins"
wins.Value = 0
wins.Parent = leaderstats

local data
local success, errorMessage = pcall(function()
	data = myDataStore:GetAsync(player.UserId)
end)

if success then
	Points.Value = data
else
	warn(errorMessage)
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
local data = myDataStore:SetAsync(player.UserId, player.leaderstats.Points.Value)
end)

if success then
	print("Data Saved")
else
	warn(errorMessage)
end

end)

game:BindToClose(function()
for i, player in pairs(game.Players:GetChildren()) do
player:Kick(“Server Closed”)
end

wait(2)

end)

1 Like

You have to give more info. Are you trying to make a script that displays your wins on screen? or something else?

Haven’t or can’t try it because its mid night but which part isn’t working. Check f9 or console for errors and in which line.

you are making a variable
try change to this

myDataStore:SetAsync(player.UserId, player.leaderstats.Points.Value)

remove the “local Data”

Are you trying it only by yourself? it could be because you are the only player, and whenever you leave, the server close before the game even saves your data. To fix it, in your bind to close, loop through all your players and then save their data. It should stop the server from closing and save the last players data.

game:BindToClose(function()
    for i, player in pairs(game.Players:GetChildren()) do
        --save 'player' data here
    end
end

Its a points system but i also want to implement wins

implementing this just killed it

this doesnt fix the data store system im so confused

what do you mean?

asd

No Errors which is strange in the developer console

i put this in and it just broke it

what do you mean by “broke”, what happened? did it output any errors?

1 Like

well did you turn on API Service yet

1 Like

where are you referencing the script? Is it taken from someone or completely did on your own. Old scripts stop working because of references being taken out of studio

1 Like

Yes its turned on so it should work if the code is right

It removed the side bar leaderstats from the top and all the things

Its wrote from a yt tutorial of someone called tyler codes ?

1 Like

Code might be out of date. Ill check in the morning if your problem hasnt been fixed by then

1 Like

can i see what you did in the bindtoclose?

1 Like

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“MyDataStore”)

game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = plr

local points = Instance.new("NumberValue")
points.Name = "Points"
points.Value = 0
points.Parent = leaderstats

local wins = Instance.new("NumberValue")
wins.Name = "Wins"
wins.Value = 0
wins.Parent = leaderstats

local data
local success, errorMessage = pcall(function()
	data = myDataStore:GetAsync(player.UserId)
end)

if success then
	Points.Value = data
else
	warn(errorMessage)
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
myDataStore:SetAsync(player.UserId, player.leaderstats.Points.Value)
end)

if success then
	print("Data Saved")
else
	warn(errorMessage)
end

end)

game:BindToClose(function()
for i, player in pairs(game.Players:GetChildren()) do
player:Kick(“Server Closed”)
end

wait(2)

end)