Problems With IntValue.Changed

Hey everyone,

I’m having problems using a IntValue.Changed function. My game currently has a player added function that runs through and creates leader stats, along with updating them for any data that may be saved. I already have a folder in Server Storage that holds a IntValue containting the players cash they own, which is named the same as the player’s name. My goal is that when the player’s cash value in sever storage is changed, that the leader stat is also updated.

My problem is that when I try and update my leader stat value with the player’s updated cash value, I get ‘attempt to index nil with changed’.
image

Here’s the portion of the code I’m having trouble with:


local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")
local myDataStore = DataStoreService:GetDataStore("myDataStore")



game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats" 
	leaderstats.Parent = player
	
	print(player.Name)
	local plrMoney = ServerStorage:FindFirstChild("PlayerMoney")
	local cash = plrMoney:FindFirstChild(player.Name)
	
	local cashLeaderboard = Instance.new("IntValue")
	cashLeaderboard.Name = "Cash"
	cashLeaderboard.Parent = leaderstats
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats
	
	local cashData
	local killsData
	
	--Handles data for cash
	local success, errormessage = pcall(function()
		cashData = myDataStore:GetAsync(player.UserId.."-cash")
	end)
    if success then
		cash.Value = cashData 
		cashLeaderboard = cash.Value
    else
	    print("There was an error whilst getting your data")
		warn(errormessage)
	end
	
	--Handles data for kills
	local success, errormessage = pcall(function()
		killsData = myDataStore:GetAsync(player.UserId.."-kills")
	end)
	if success then
		kills.Value = killsData 
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
	
	cash.Changed:Connect(function(val) -- When a player's money is updated, the leaderboard is updated
	
		cashLeaderboard.Value = val

	end)
end)

I’ve just recently got back into coding and I’m a bit rusty, so I may be trying to go about this in the wrong way. Any help is appreciated!

Is there a separate script that handles creation of the “plrMoney” folder and it’s contents? Based on the error given, it appears to me that the cash variable is nil, which means FindFirstChild was not able to find anything so it returned false, or nil. Try taking care of that inside this same script prior to declaring this variable.

Yes, it’s established in another script. This is what I would have in ServerStorage when I run the game:

image

I figured there might be some latency in between that value being created, and the code searching for it so that’s why I original added the second ‘FindFirstChild’, but had the same result.

image

I tried ‘WaitForChild’ originally, but that resulted in the leaderboard not even being created. Sorry if I didn’t provide enough information.

Right, so instead of that, create that item within this same event rather than in a separate script. In the other script, you can just use .ChildAdded on your folder and then do whatever you’d like with the values. I don’t know why exactly your script isn’t finding it, but without looking at the other script, I can’t infer anything. Maybe try adding a task.wait(2) at the beginning of the event firing, or just do it like I said originally:

local DataStoreService = game:GetService("DataStoreService")
local ServerStorage = game:GetService("ServerStorage")
local myDataStore = DataStoreService:GetDataStore("myDataStore")



game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats" 
	leaderstats.Parent = player
	
	print(player.Name)
	local plrMoney = ServerStorage:FindFirstChild("PlayerMoney")
	local cash = Instance.new("IntValue",plrMoney)
        cash.Name = player.Name
	
	local cashLeaderboard = Instance.new("IntValue")
	cashLeaderboard.Name = "Cash"
	cashLeaderboard.Parent = leaderstats
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats
	
	local cashData
	local killsData
	
	--Handles data for cash
	local success, errormessage = pcall(function()
		cashData = myDataStore:GetAsync(player.UserId.."-cash")
	end)
    if success then
		cash.Value = cashData 
		cashLeaderboard = cash.Value
    else
	    print("There was an error whilst getting your data")
		warn(errormessage)
	end
	
	--Handles data for kills
	local success, errormessage = pcall(function()
		killsData = myDataStore:GetAsync(player.UserId.."-kills")
	end)
	if success then
		kills.Value = killsData 
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
	
	cash.Changed:Connect(function(val) -- When a player's money is updated, the leaderboard is updated
	
		cashLeaderboard.Value = val

	end)
end)

Change Instance.new to IntValue.new and add a Int value variable, with Instance.new you’re creating a new value for kills or cash, with changing it to IntValue.new it should update the current one. Also you should put it in a loop because from what I can see you don’t have it in a loop. (For more clarification, right now the code will only run once putting it in a loop such as (while true do) will constantly be updating these stats as they change)

1 Like

Yeah, I get I’m working in a very backwards way. This is because the other script is linked and referenced throughout so many other scripts that I’m trying to find a workaround. I’ll try task.wait, thanks.

I didn’t need to make any changed to the instances, just needed to add a loop. This worked for me:

I appreciate the help! I’m just hoping actively running this with just a 0.5 wait in the loop won’t cause too many performance issues. It’s also interesting to me that when I run this with a single ‘FindFirstChild’, the value comes back nil, but having 2 seems to work. Such as:

image

Do you know if latency could break this script whilst using FindFirstChild?

Sorry about the late reply. Ah ok, no problem, hopefully not but if it does if your game is match based you could update it at the end of a match, that’s weird idk why I’m not very skilled, I have no idea I would guess not.