Donation Data not saving

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)
2 Likes

What is Activity_Value

30

2 Likes

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'.

1 Like

Why do you have a

local Player = Players.LocalPlayer

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

2 Likes

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?)

2 Likes

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.

1 Like

Alright, thanks for letting me know.

1 Like

You can not use LocalPlayer in a server script, you already define Player in the PlayerAdded function.

Also, this part has a random space:

I am not sure if that was just an error when you were pasting the code over.

Exactly what I was saying

30

You cannot use localplayer in a server script. Wrap SetAsync function in a pcall.

1 Like

@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

1 Like

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.

2 Likes

What happens when you print out the Donation_Value variable

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…

Lol I guess is scripters keep thinking the same things because I just said that 1 second befor lol

What you said could very well be the problem @DaffyDavinko should definitely use WaitForChild

3 Likes

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.

1 Like

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

It is marked as a solution though…