Hi. So i made an NPC that gives you a sack, and i made some wheats then added ProximityPrompt to these wheats. What i want is when 10 of these wheats are collected by triggering the ProximityPrompt, the name of the sack turns to “Full Sack”, and you cant get wheat into this sack anymore. I really dont know where to start, i tried to do that with NumberValue stuff and it didnt worked. If anyone can help, thanks a lot!
Have you tried putting a numbervalue in somewhere like the player?
So if I understand correctly,
- Every time you “finish” the proximity prompt you gain 1 wheat,
- The sack can only hold 10 wheat.
That means you can use ProximityPrompt.Triggered | Roblox Creator Documentation to check when they have gathered the wheat. Where ever you store the amount of wheat they got should now be incremented IF that value is <10
.
I tried to put one in the wheat, and i dont know it didnt worked. I dont know how to put one in player, or if even i know the wheat one doesnt worked so i think it will not work too
Sidenote: Avoid using numbervalues if possible, they are quite slow compared to Instance Attributes (roblox.com) (which often make more sense to use anyways).
I tried same thing, it didnt work. I tried to check if the number is smaller then 10, it adds 1 to the number. But nothing worked
Would you be able to send the code? Much easier to help find potential issues with some code as reference
Sure, let me write the code again
Here is a basic example of the code i tried:
local promt = script.Parent.ProximityPrompt
local value = script.Parent.Value.Value
promt.Triggered:Connect(function(plr)
if plr.Backpack:FindFirstChild("Sack") then
if value < 10 then
value = value + 1
end
end
end)
when you set this variable local value = script.Parent.Value.Value
it is copied because Value.Value
is a base type. To get a reference use local numberValue = script.Parent.Value
and change the use of this variable to numberValue.Value
local promt = script.Parent.ProximityPrompt
local numberValue = script.Parent.Value
promt.Triggered:Connect(function(plr)
if plr.Backpack:FindFirstChild("Sack") then
if numberValue.Value < 10 then
numberValue.Value += 1
end
end
end)
It works now, thank you! I didnt know the problem was that.
Ah, so basically, the variable value
will have the value that script.Parent.Value
had when you asked for it’s value. If you do this instead: (read @gertkeno’s answer)
I would however not advice using NumberValue
, instead you could store the value on the sack (Which makes sense, since the sack is the one carrying the wheat). To do that you would write this:
local prompt = script.Parent.ProximityPrompt -- Changed promt -> prompt
local value = script.Parent.Value.Value
prompt.Triggered:Connect(function(plr)
local sack = plr.Backpack:FindFirstChild("Sack")
if sack then
local currentWheat = (sack:GetAttribute("Wheat") or 0)
if currentWheat < 10 then
sack:SetAttribute("Wheat", currentWheat + 1)
end
end
end)
This is a good option too thanks!