To give some context, I have a game where players go around picking up ores; like diamonds in this case. Upon joining the game, the new player will get a folder called "orestats" and inside this folder there are a bunch of IntValues to count the player's number of each ore. When the player touches an ore, like a diamond, the script inside the diamond locates the player's IntValue counter for that specific ore and adds one to the counter, before the ore is destroyed.
The part I'm having trouble with is adding 1 to the player's PlayerGui counter. Before the ore is destroyed, the script locates the TextLabel - "DiamondAmount" - inside the player's PlayerGui and changes the text to show the IntValue's (diamond counter's) number. This is to show the player how much of the certain ore they have, since they can't see the IntValue's value while in game. I'm not sure if you can set a TextLabel's Text to an IntValue's value, so I'm not sure why it's not working. Maybe you can help?
This is the script inside the diamond ore that the player picks up upon touching it:
function onTouch(part) local h = part.Parent:findFirstChild("Humanoid") if (h~=nil) then local thisplr = game.Players:findFirstChild(h.Parent.Name) if (thisplr~=nil) then local diamondscore = thisplr.orestats:findFirstChild("DiamondNum") diamondscore.Value = diamondscore.Value + 1 -- This is the part I'm having trouble with: local diamondcounter = thisplr.PlayerGui.OreInventory.MainBox.DiamondCount.DiamondAmount.Text diamondcounter = diamondscore.Value end script.Parent:Destroy() end end script.Parent.Touched:connect(onTouch)
function onTouch(part) local h = part.Parent:findFirstChild("Humanoid") if (h~=nil) then local thisplr = game.Players:findFirstChild(h.Parent.Name) if (thisplr~=nil) then local diamondscore = thisplr.orestats:findFirstChild("DiamondNum") diamondscore.Value = diamondscore.Value + 1 -- This is the part I'm having trouble with: local diamondcounter = thisplr.PlayerGui.OreInventory.MainBox.DiamondCount.DiamondAmount.Text diamondcounter = diamondscore.Value end script.Parent:Destroy() end end script.Parent.Touched:connect(onTouch)
The problem is that you have declared "diamondcounter" as the text, rather than the gui object, meaning will be a string value rather than the object.
To fix this, replace it with:
local diamondcounter = thisplr.PlayerGui.OreInventory.MainBox.DiamondCount.DiamondAmount diamondcounter.Text = diamondscore.Value
The thing is, it's storing what it is currently rather than what it is in real-time. For example;
I have 5 diamonds. I ask someone to remember that I have those 5. I then go out and I find 3 more. I now have 8 diamonds. This person does not know how many I obtained, so if I ask him how many I have, he will say "5".
An easy way to prevent this is to use a Changed
event. Using the Changed event will update the TextLabel's text based on what was changed.
script.Parent.Touched:connect(function(hit) --This is a much easier way to start functions local hum=hit:FindFirstChild("Humanoid") --This will find the humanoid if hum then --If the humanoid is there.. local player=game.Players:GetPlayerFromCharacter(hum.Parent) --This will get the player from game.Players if player then --If player is there.. local diamonds=player.orestats:FindFirstChild("DiamondNum") diamonds.Value=diamonds.Value+1 end end end)
In a LocalScript, do this.
local player=game.Players.LocalPlayer local diamond = player.orestats.DiamondNum local score=player.PlayerGui.OreInventory.MainBox.DiamondCount.DiamondAmount diamond.Value.Changed:connect(function(value) --This will fire when the player's diamond value changes. "value" is equal to the new value. score.Text=value --This will change the Text to the new value every time the diamond score changes. end)