GetPropertyChangedSignal() doesn't work

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a script that changes the value of a NumberValue when the NumberValue is bigger than the MaxAmount.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is that it doesn’t work.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I couldn’t find any solutions.

-- Services
local Players = game:GetService("Players")

-- Variables
local MaxGold = 100000000000
local MaxDiamonds = 500000

-- Functions
Players.PlayerAdded:Connect(function(Player)
	-- Values - Folders
	local DataFolder = Player:FindFirstChild("DataFolder")
	
	local Gold = DataFolder.Gold
	local Diamonds = DataFolder.Diamonds
	
	-- :PropertyChangedSignal() - Conditions
	Gold:GetPropertyChangedSignal("Value"):Connect(function()
		if Gold.Value > MaxGold then
			Gold.Value = MaxGold
		elseif Gold.Value < 0 then
			Gold.Value = 0
		end
	end)
	
	Diamonds:GetPropertyChangedSignal("Value"):Connect(function()
		if Diamonds.Value > MaxDiamonds then
			Diamonds.Value = MaxDiamonds
		elseif Diamonds.Value < 0 then
			Diamonds.Value = 0
		end
	end)
end)

Also, here is the error: lua ServerScriptService.Data.Currency.CurrencySecurity:13: attempt to index nil with 'Gold'

1 Like

The error seems like gold doesn’t exist, so I would check to see if the values exist in game. If they don’t add do local Gold = DataFolder:WaitForChild("Gold")

1 Like

Currency Script:

-- Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

-- Data Stores
local GoldData = DataStoreService:GetDataStore("GoldCoinsDataStore")
local DiamondsData = DataStoreService:GetDataStore("Diamonds")

-- Functions
Players.PlayerAdded:Connect(function(Player)
	-- Instances
	local DataFolder = Instance.new("Folder", Player)
	DataFolder.Name = "DataFolder"
	
	local Gold = Instance.new("NumberValue", DataFolder)
	Gold.Name = "Gold"
	
	local Diamonds = Instance.new("NumberValue", DataFolder)
	Diamonds.Name = "Diamonds"
	
	-- Data Table
	local Data
	
	pcall(function()
		Data = {
			GoldData:GetAsync(Player.UserId);
			DiamondsData:GetAsync(Player.UserId);
		}
	end)
	
end)

Players.PlayerRemoving:Connect(function(Player)
	-- Data Table
	local Data = {
		Player.DataFolder.Gold.Value;
		Player.DataFolder.Diamonds.Value;
	}
	
	local success, errormessage = pcall(function()
		GoldData:SetAsync(Player.UserId, Data[1])
		DiamondsData:SetAsync(Player.UserId, Data[2])
	end)
	
	if success then
		print("Successfully saved data for "..Player.Name.."(#"..Player.UserId..")")
	else
		warn(errormessage)
	end
end)

image


Now, I do not get any error but the Gold/Diamond value doesn’t change to MaxGold.

1 Like

Are you sure that this value change happened on the server not the client?

I did the change on the client, and that seems to be the problem.
But also, for some reason I got an error again, but I didn’t change anything.

Error: lua ServerScriptService.Data.Currency.CurrencySecurity:13: attempt to index nil with 'WaitForChild'

Line 13/14:

local Gold = DataFolder:WaitForChild("Gold")
local Diamonds = DataFolder:WaitForChild("Diamonds")

The data folder is created after this code executes. Replace the first line in the function with:

local DataFolder = Player:WaitForChild("DataFolder")

I assume you create the folder in another connection.

Change

local DataFolder = Player:FindFirstChild("DataFolder")

To this

local DataFolder = Player:WaitForChild("DataFolder")

This will fix it.

2 Likes

Thanks so much @msix29 and @cnr123451
It worked!

2 Likes
Gold.Changed:Connect(function(NewGold)
	Gold.Value = math.clamp(NewGold, 0, MaxGold)
end)

Diamonds.Changed:Connect(function(NewDiamonds)
	Diamonds.Value = math.clamp(NewDiamonds, 0, MaxDiamonds)
end)

Just a few improvements.