Help with getPropertyChangedSignal

Hello all!
I am currently working on a tycoon game, and I want the buy buttons to turn green when the players money equals to or is above the cost of that certain item. This is the code I have right now

local MainScripts = require(script.Parent.Parent.Brain)
local Cost = 100
local AttachedPart = script.Parent.Attached.Value
local player
local FoundOwner = false

game.Teams["Vorld Wision"].PlayerAdded:Connect(function(plr)
	player = plr
	FoundOwner = true
	player.leaderstats.Money += 10
end)


script.Parent.Top.Touched:Connect(function(plr)
		
		local CanBuy = MainScripts.CheckIfCanBuy(Cost, plr)
		if CanBuy == true  then
			script.Parent.Top:Destroy()
		script.Parent.Bot:Destroy()
		script.Parent.BillboardGui:Destroy()
		AttachedPart.CargoDoorFL.Prox.ProximityPrompt.Enabled = true
		MainScripts.Appear(AttachedPart)
		end
end)



player.leaderstats.Money.Changed:Connect(function()
	print("IT WORKED")
	if player.leaderstats.Money >= Cost  then
		script.Parent.Top.BrickColor = BrickColor.new("Lime green")
	end
	
end)

I suspect that it has something to do with the order that the module script finds the player but I have no clue. here is that

local Buttons = {}
local Team = script.Parent.SpawnLocation.TeamColor
local player = nil

game.Teams["Vorld Wision"].PlayerAdded:Connect(function(plr)
player = plr		

end)

function Buttons.Appear(mod)
	for _, v in pairs(mod:GetDescendants()) do
		if v:IsA("Part") then
			v.Transparency = 0
		v.CanCollide = true
		v.CanTouch = true
		end
		
	end
end

function Buttons.CheckIfCanBuy(Cost, plr)
	if (game.Players:GetPlayerFromCharacter(plr.Parent).TeamColor == script.Parent.SpawnLocation.TeamColor) and (game.Players:GetPlayerFromCharacter(plr.Parent).leaderstats.Money.Value >= Cost) then
		game.Players:GetPlayerFromCharacter(plr.Parent).leaderstats.Money.Value = game.Players:GetPlayerFromCharacter(plr.Parent).leaderstats.Money.Value - Cost
		return true
	else
		return false
	end
		
end

function Buttons.FindOwner()
	return player
end

Thanks to anyone that can help!

1 Like

What is each type of script for the code?
And second, in the second set of code, how are the functions in it being triggered? Via the client or server?

  1. The first is a normal script located inside of the buy button while the second is a module script located inside of the tycoon model.
  2. those functions are called mainly in the first script; I haven’t finished using them all or creating them :sweat_smile:

if this helps the error that I am getting is : Workspace.UL.BuyButton.Script:28: attempt to index nil with ‘leaderstats’. This only happens because it finds the player after they’ve joined the tycoon, Although that shouldn’t matter because after the player is found the getPropertyChangedSignal should still be called anytime the money value changes, unless it just completely ruins the script.

which script does this error get triggered in?

the first one,

image

For starters you arent event using getpropertychangedsignal, but dont worry I got ya!

Lets begin! First off the error is probably occurring due to you trying to connect the function to leaderstats, ofc its nil at the time SOOOO

player:WaitForChild("leaderstats",30).Money.Changed:Connect(function()
end)

with the power of wait for child we can make sure we wait for the thing to load in that way there arent any problems!
Next we need to actually use GetPropertyChangedSignal sooo

player:WaitForChild("leaderstats",30).Money.GetPropertyChangedSignal("Value"):Connect(function()
end)

PS: Probably type the things out yourself I might have made some typos

I’m I actually so stupid. I was making changes to the code trying to figure out the issues with the original getPropertyChangedSignal. I never changed it back! Thanks a lot let me see if this works!

2 Likes

Unfortunately, it did not work. Here is the error : Workspace.UL.BuyButton.Script:28: attempt to index nil with ‘WaitForChild’

here is the new code so you can make sure I’m not stupid.

I’m pretty sure the reason that that error occurs is because it attempts to look for the player before it’s actually defined. I dont think that it matters though, this is because after the player joins the tycoon it gets defined(I have tested this).

Pretty easy error to read, player is nil when that line runs.

player doesn’t instantiate until after your PlayerAdded connection, so move it inside there instead. Also make sure you are disconnecting any left over connections when the player has left to prevent memory leak.

1 Like

Thanks! I knew what the error meant, but I had no idea that it would continue to check if the signal changes inside of the player added

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