Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

I can't change the int value even though the script knows what it is?

Asked by 8 years ago

I've been working on an inventory system and when the player places an item down, the value in their inventory is supposed to decrease. However, this doesn't happen, and I'm not sure why.

The code I'm using:

wait(3)
local playername = script.Parent.Parent.PlayerNameTag.Value
local haveitem = game.Players:FindFirstChild(playername).Character.OwnedItems.BlueShrine.Value

if haveitem == 0 then
    script.Parent.Parent:Destroy()
end

if haveitem >= 1 then
    haveitem = (haveitem - 1)
end

The thing is... the script still knows what the value of BlueShrine is, as it still destroys the model when haveitem is 0. Does anybody know a solution? Am I making an obvious mistake?

0
Can you show us any errors it prints out when you try to do it? MrLonely1221 701 — 8y
0
No errors print out. coolyoshipower 42 — 8y

2 answers

Log in to vote
2
Answered by 8 years ago

The problem is that this code will return the value e.g 5 not the object which stores the value in:-

local haveitem = game.Players:FindFirstChil(playername).Character.OwnedItems.BlueShrine.Value

So all you are doing is changing the value of the variable and not the object.

Simple fix to get the object:-

wait(3)
local playername = script.Parent.Parent.PlayerNameTag.Value
local haveitem = game.Players:FindFirstChild(playername).Character.OwnedItems.BlueShrine

if haveitem.Value == 0 then
    script.Parent.Parent:Destroy()
end

if haveitem.Value >= 1 then
    haveitem.Value = (haveitem.Value - 1)
end

Hope this helps

0
In my experience with scripting having the .Value in either place doesn't change how it works. Sometimes it does run better with the .Value like this though so it should help. MrLonely1221 701 — 8y
0
Ah, this makes A LOT of sense! Thanks, I'm going to try it now. coolyoshipower 42 — 8y
0
It works! Thanks for the help! coolyoshipower 42 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

You are missing the .Value

if haveitem.Value == 0 then
        script.Parent.Parent:Destroy()
    end
    if haveitem.Value >= 1 then
        haveitem.Value = haveitem .Value- 1
end

Answer this question