[UNSOLVED] How to detect if a value inside any player changes

Hi, I have a folder of values that is cloned into each players for their data. I’m trying to make a dictionary in a module script for values that have been changed, and will save to the datastore. This is what I have so far, but it’s not working and not giving any errors.

for i, v in pairs(game.Players:GetChildren()) do
	local updateModule = require(game.ReplicatedStorage:FindFirstChild("UpdateData"))
	local toUpdate = updateModule.toUpdate
	local folder = v:FindFirstChild("PlayerDataFolder")
	local gameslot = folder.CurrentGame.Value
	
	gameslot = 0
	
	folder.CurrentGame.Changed:Connect(function()
		if updateModule.dataLoaded[v] == true then
			if folder.CurrentGame.Value >= 1 then
				gameslot = folder.CurrentGame.Value
			end
		end
	end)

	folder.HasPlayed.Changed:Connect(function()
		if updateModule.dataLoaded[v] == true then
			print("value changed!")
			toUpdate[v .. "HasPlayed"] = folder.HasPlayed.Value
			print(toUpdate)
			dataSave(v, toUpdate)
		end
	end)
end
1 Like

Did you try using GetPropertyChangedSignal ?

1 Like
Value.Changed:Connect()

--or

Value:GetPropertyChangedSignal("Value") --detects only if the value changed
1 Like

I’m not 100% I did this right, but it’s still not working for me

for i, v in pairs(game.Players:GetChildren()) do
	local updateModule = require(game.ReplicatedStorage:FindFirstChild("UpdateData"))
	local toUpdate = updateModule.toUpdate
	local folder = v:FindFirstChild("PlayerDataFolder")
	local gameslot = folder.CurrentGame.Value
	
	gameslot = 0
	if updateModule.dataLoaded[v] == true then 
		
		local function hasPlayed(newValue)
			print("value changed!")
			toUpdate["HasPlayed"] = folder.HasPlayed.Value
			print(toUpdate)
			dataSave(v, toUpdate)
		end
	
	
		local function currentGameChanged(newValue)
			if folder.CurrentGame >= 1 then
				gameslot = folder.CurrentGame.Value
			end
		end
		folder.HasPlayed:GetPropertyChangedSignal():Connect(hasPlayed)
		folder.CurrentGame:GetPropertyChangedSignal():Connect(currentGameChanged)
	end
end
1 Like

My original script worked fine before I added the ‘for i, v in pairs’ around it

1 Like

You forgot to put the property you want to detect being changed
image

GetPropertyChangedSignal(“Your Property”)

1 Like

I did, thank you! But it’s still not detecting the change unfortunately.

If I change it to only detect when a specific player’s value changes, it works fine. But with the ‘for i, v in pairs(game.Players:GetChildren()) do’ it doesn’t work.

1 Like

I’m assuming from quickly reading your code that you’re making some sort of system that keeps track of weather a Player has played on a certain map / gamemode (something along them lines) and what you’re trying to accomplish is a loop that connects a PropertyChangedSignal event to the individual Player that you want to change the value of only changing their value?

If so, maybe try linking it to PlayerAdded and PlayerRemoving events like so:

local PlayersAlreadyInGame = {}
game.Players.PlayerAdded:Connect(function(player)
	if not table.find(PlayersAlreadyInGame, player.Name) then
		table.insert(PlayersAlreadyInGame, player.Name)
		
		local coins = player:WaitForChild("leaderstats").Coins -- change this to whatever value you want to change

		coins:GetPropertyChangedSignal("Value"):Connect(function() -- make sure this the correct property you're listening for
			print("coins value changed") -- run the code you want to fire when the value changes
		end)
		
	end
end)

game.Players.PlayerRemoving:Connect(function(player) -- this event just removes the player from the PlayersInGame table to stop events from stacking
	if table.find(PlayersAlreadyInGame, player.Name) then
		table.remove(PlayersAlreadyInGame, table.find(PlayersAlreadyInGame, player.Name))
		print(player.Name.." removed from the table")
	end
end)

this code worked for me, when you change the value inside of any Player it would fire from the Server changing the value for everyone else too (without event stacking)

make sure this code is inside of a ServerScript (of course) and if there is any errors let me know! :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.