Value changed won't work

Hey developers. Here is my script, I don’t understand why it’s won’t work.

game.Players.PlayerAdded:Connect(function(plr)
	plr.GameData.Experience.Value.Changed:Connect(function()
		print("UCF DATA CHANGE\nData changed: Experience\nNew data: "..plr.GameData.Experience.Value)
	end)
end)

game.Players.PlayerAdded:Connect(function(plr)
	plr.GameData.CardLevel.Value.Changed:Connect(function()
		print("UCF DATA CHANGE\nData changed: Card Level\nNew data: "..plr.GameData.CardLevel.Value)
	end)
end)

When detecting a particular property that changes you will want to do :GetPropertyChangedSignal(property). So you would do:

game.Players.PlayerAdded:Connect(function(plr)
	plr.GameData.Experience:GetPropertyChangedSignal("Value"):Connect(function()
		print("UCF DATA CHANGE\nData changed: Experience\nNew data: "..plr.GameData.Experience.Value)
	end)
end)

game.Players.PlayerAdded:Connect(function(plr)
	plr.GameData.CardLevel:GetPropertyChangedSignal("Value"):Connect(function()
		print("UCF DATA CHANGE\nData changed: Card Level\nNew data: "..plr.GameData.CardLevel.Value)
	end)
end)

Assuming that Value is a property and not the name of something else

Does not work. I tried changing my level, didn’t work.

You can remove Value property at Changed event and it should work. Such as:

game.Players.PlayerAdded:Connect(function(plr)
	plr.GameData.Experience.Changed:Connect(function()
		print("UCF DATA CHANGE\nData changed: Experience\nNew data: "..plr.GameData.Experience.Value)
	end)

    plr.GameData.CardLevel.Changed:Connect(function()
		print("UCF DATA CHANGE\nData changed: Card Level\nNew data: "..plr.GameData.CardLevel.Value)
	end)
end)

Is value a property or name of something?

Still not working, hmmmmmmmmmm

Where are you changing the value? If you’re changing it from client, and you’re checking for changes on the server, nothing will happen.

I’m checking for changes on the server. That’s whyy.

But I’m changing it on the server.

Here’s the data script that is changing it:

local data = game:GetService("DataStoreService"):GetDataStore("gamedata_112NSDNFD")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "GameData"
	leaderstats.Parent = plr 
	
	local XP = Instance.new("IntValue")
	XP.Name = "Experience"
	XP.Value = 0
	XP.Parent = leaderstats
	
	local UCFLevel = Instance.new("IntValue")
	UCFLevel.Name = "CardLevel"
	UCFLevel.Parent = leaderstats
	UCFLevel.Value = 0
	
	local Overseer = Instance.new("BoolValue")
	Overseer.Name = "IsOverseer"
	Overseer.Parent = leaderstats
	
	local Division = Instance.new("StringValue")
	Division.Name = "DivisionOverseer"
	Division.Parent = Overseer
	Division.Value = "N/A"
	
	local ExperienceData
	local cLvlData
	local success, errormessage = pcall(function()
		ExperienceData = data:GetAsync(plr.UserId.."-experience")
		print("exp data")
		cLvlData = data:GetAsync(plr.UserId.."-cLvl")
		print("card data")
	end)
	if success then
		plr.GameData.Experience.Value = ExperienceData
		plr.GameData.CardLevel.Value = cLvlData
		print("\nUCF DATABASE\nPlayer "..plr.Name.."'s data has been loaded.")
	else
		warn("\nUCF DATABASE\nPlayer "..plr.Name.."'s data could not be loaded. Error: "..errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormessage = pcall(function()
	data:SetAsync(plr.UserId.."-experience", plr.GameData.Experience.Value)
	data:SetAsync(plr.UserId.."-cLvl", plr.GameData.CardLevel.Value)
	end)	
	if success then
		print("UCF DATABASE\nPlayer "..plr.Name.."'s data has been saved.")
	else
		warn("UCF DATABASE\nPlayer "..plr.Name.."'s data could not be saved. Error: "..errormessage)
	end
end)

Where is the script listening for the value changes located?

It’s listening inside a folder called “GameData” for any value changes.
Edit: Screenshot by Lightshot

Scripts wont run under the player instance, except if they’re parented inside the player backpack(and LocalScripts only run in PlayerGui or PlayerScripts).

Should I put my script in the player backpack then?

Also, (I know it’s off topic) but my datastore does not seem to save, even tho it’s prints all my messages.

Personally I consider putting scripts under backpack a bad practice(except if it’s related to a tool), the code snippet provided above can run inside ServerScriptService with a few changes applied:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.GameData.Experience.Changed:Connect(function(new)
		print("UCF DATA CHANGE\nData changed: Experience\nNew data: "..new)
	end)
	player.GameData.CardLevel.Changed:Connect(function(new)
		print("UCF DATA CHANGE\nData changed: Card Level\nNew data: "..new)
	end)
end)
1 Like