Not Detecting Property Changed Signal

Trying to make it so it detects when the XP value changes. I’ve tried doing xpValue.Value, didn’t work. I’ve tried xpValue.Changed as well. Unsure what to do.

xpValue:GetPropertyChangedSignal("Value"):Connect(function(newVal)
	
	print("New XP value.")
end)
1 Like

Is xp value loaded and ready to be referenced?

Yes.

It works once at the start when the game loads, but never again.

Could you send the full script and any errors?


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

local plr = game.Players.PlayerAdded:Wait()
local xpValue = plr:FindFirstChild("XP_Count")
-- DataStore
local XPDataStore = DataStoreService:GetDataStore("StoreXP")


local Value = 0


Players.PlayerAdded:Connect(function(player)

	local xpCount = Instance.new("IntValue")
	xpCount.Name = "XP_Count"
	xpCount.Value = 0 
	xpCount.Parent = player


end)

game.Players.PlayerAdded:Connect(function(player)
	-----------------------------------------------------------
	local cash= player:WaitForChild("XP_Count")

	cash.Parent = player
	cash.Value = 0 
	cash.Value = XPDataStore:GetAsync(player.UserId) or Value
	XPDataStore:SetAsync(player.UserId, cash.Value)
	-----------------------------------------------------------

	------------------------------------------------------------
	game.Players.PlayerRemoving:Connect(function(player)
		XPDataStore:SetAsync(player.UserId, player["XP_Count"].Value)

	end)

end) 





xpValue:GetPropertyChangedSignal("Value"):Connect(function(newVal)
	
	print("New XP value:", newVal)
end)

How are you changing the value? Where is that script located?

Why are you referencing it outside of the whole playeradded statement? And I’m guessing this is a server script because your using datastore

1 Like

The value is changed through a local script within a tool. The script is located in the tool, the code that I gave above is in Server Script.

Updated script


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

local plr = game.Players.PlayerAdded:Wait()
-- DataStore
local XPDataStore = DataStoreService:GetDataStore("StoreXP")


local Value = 0


Players.PlayerAdded:Connect(function(player)

	local xpCount = Instance.new("IntValue")
	xpCount.Name = "XP_Count"
	xpCount.Value = 0 
	xpCount.Parent = player

   xpCount:GetPropertyChangedSignal("Value"):Connect(function(newVal)
       print("New XP value:", newVal)
   end)


end)

game.Players.PlayerAdded:Connect(function(player)
	-----------------------------------------------------------
	local cash= player:WaitForChild("XP_Count")

	cash.Parent = player
	cash.Value = 0 
	cash.Value = XPDataStore:GetAsync(player.UserId) or Value
	XPDataStore:SetAsync(player.UserId, cash.Value)
	-----------------------------------------------------------

	------------------------------------------------------------
	game.Players.PlayerRemoving:Connect(function(player)
		XPDataStore:SetAsync(player.UserId, player["XP_Count"].Value)

	end)

end) 

I’m unsure why, but your script isn’t working.

Okay your saying the value is changed through a local script, there either two things that means your either changing the value on the client without a remote event or your using as remote event to fire to the server that you want to change the XP and then it changes the XP

so if its not working then im going to guess your trying to do it with the former, but you havent showed any client code so we cant know that for sure

There is no remote event.

This is the tool script

tool.Activated:Connect(function()
	local player = game.Players:GetPlayerFromCharacter(tool.Parent)
	if player then
		local xp = player:FindFirstChild("XP_Count")
		xp.Value = xp.Value + 1
	end
end)

Yeah well thats why it isnt working you can’t just reference the players XP_Count from the client and then expect it to change and replicate to the server you need to fire a remote event and then have the server script handler handle changing the XP for the player

Updated code

-- Client Script
local RemoteEvent = game.ReplicatedStorage.RemoteEvent

tool.Activated:Connect(function()
	local player = game.Players:GetPlayerFromCharacter(tool.Parent)
	if player then
      RemoteEvent:FireServer()
	end
end)


-- Server Script
local RemoteEvent = game.ReplicatedStorage.RemoteEvent

RemoteEvent.OnServerEvent:Connect(function(player)
   player:FindFirstChild("XP_Count").Value += 1
end)

ServerScriptService.Script:49: attempt to index nil with ‘Value’

This means that it cant find the XP Value for some odd reason try to add a WaitForChild

local xp = player:WaitForChild("XP_Count")
xp.Value += 1

The XP_Count is no longer in the player, and I’m now getting an error.
Infinite yield possible on ‘Players.Comqts:WaitForChild(“XP_Count”)’

Well, why isn’t it in the player you have a server script that puts it inside of the player when they join, why wouldnt it be there

I do not know the reasoning for it not being there.

Do you have another script that is possibly deleting it or something?