So I am trying to save and load the data of the player using the script below, and the script below is the same that I used for Currenct saving and other stats, and I do not know why it acts diffrent this time. The error that I am getting is this, ServerScriptService.StatsManager.Donations:31: attempt to index nil with '.Changed', meaning the fact that the .Changed event is not a part of a IntValue??
local DataStoreService = game:GetService("DataStoreService");
local Players = game:GetService("Players");
local Player = Players.LocalPlayer
local DataBase = {
DonationData = DataStoreService:GetDataStore("DonationStore"),
};
Players.PlayerAdded:Connect(function(Player)
-->> Data
local _Donate= DataBase.DonationData:GetAsync(Player.UserId);
local Donation_Value = Player.leaderstats:FindFirstChild("DonationAmount");
if _Donate ~= nil then
Donation_Value .Value = _Donate
end
Donation_Value.Changed:Connect(function(ChangedVal)
DataBase.DonationData:SetAsync(Player.UserId, ChangedVal)
end)
end)
Oh sorry copied the wrong script, updated, but the problem still occurs with the following error ServerScriptService.StatsManager.DataHandlers.Donation:17: attempt to index nil with 'Value'.
This is a ServerScript you cannot use local player. Plus I don’t see you using that and you might be confusing the script with the “Player” your using because you have the same name as parameter so delete that
What I’m confused about is why the error you sent us says line 31, but there isn’t 31 lines. Are you not showing the whole script?
(or was the error you sent afterward the right one?)
So I didn’t show the whole script since there was script data like the Script’s Name, Author, Date it was created which I thought isn’t very important or needed.
@domboss37 Is right in the fact that you can’t access the local player in a server script, though I’m not even sure what purpose the player variable is for.
I think your problem is that Donation_Value doesn’t exist, have you double checked that it does?
Not their problem though, localplayer has no meaning in the script we’re seeing and does not pertain to the error at all.
They want a solution to their problem, not a code review
Do you create the “DonationAmount” value in another script? You should probably do it in the same script instead, I believe the value is being created after the line where you declare the variable referencing it so it does not exist yet. You could also use WaitForChild, but creating it in the same script just makes more sense.
local DataStoreService = game:GetService("DataStoreService");
local Players = game:GetService("Players");
local DataBase = {
DonationData = DataStoreService:GetDataStore("DonationStore"),
};
Players.PlayerAdded:Connect(function(Player)
-->> Data
local _Donate = DataBase.DonationData:GetAsync(Player.UserId)
local Donation_Value = Player.leaderstats:FindFirstChild("DonationAmount");
print(Donation_Value)
if _Donate ~= nil then
Donation_Value.Value = _Donate
end
Donation_Value.Changed:Connect(function(ChangedVal)
DataBase.DonationData:SetAsync(Player.UserId, ChangedVal)
end)
end)
For debugging, try this. Reply with what happens @domboss37 beat me to it…
Okay so I figured it out in the end with the help, thank you all for replying to this post. Also if your curious on what I added, basically moved the code to the leaderstats script, and added the :WaitForChild() as @lifacy suggested.
I’m not trying to stir anything but if he helped you get there you should probably mark it solved with his help. I’m New here and it’s easier to browse and shows the better answers first