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
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?
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.
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
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!
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.