Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

2 comments

0

Can you show us any errors it prints out when you try to do it?

0

No errors print out.

2 answers

✓ Accepted answer

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

3 comments

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.

0

Ah, this makes A LOT of sense! Thanks, I'm going to try it now.

0

It works! Thanks for the help!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.