Hello, I am currently making a script that prints out the price of a gun which is a string value under the gun.
I keep getting the error “Attempting to index nil with price”, I know this means it does not exist but I can scroll through replicated storage and see it there myself.
I have tried :FindFirstChildOfClass() to print it, :WaitForChild(), :FindFirstChild()
Here are some photos and the script
local name = script.Parent.Text
local gun = game:GetService("ReplicatedStorage").Storage:FindFirstChild(name)
script.Parent.MouseButton1Click:Connect(function()
print(gun.Price.Value)
end)
It could be that you have to retrieve the text when you click as the first time it will be empty/the default text, as nothing updates the name variable with the current text, try this?
script.Parent.MouseButton1Click:Connect(function()
local name = script.Parent.Text
local gun = game:GetService("ReplicatedStorage").Storage:FindFirstChild(name)
print(gun.Price.Value)
end)
Edit: Just realised this is a textbutton, @NotDerpyDemon are you sure you wrote the gun’s name correctly in the text property?
local name = script.Parent.Text
local gun = game:GetService("ReplicatedStorage")
script.Parent.MouseButton1Click:Connect(function()
print(gun.Storage.AK74M.Price.Value)
end)
I know this post has already been solved and all, but if you’re planning on adding more guns, making more localscripts and just tweaking the gun to a different gun isn’t going to be practical as you’d have tons of localscripts which would make adding a change difficult/repetitive. What did you originally write in the Text property? I think it would’ve been better to rename your Button the same as the Gun it’s related to and get the name of the button instead, something like this after you change the name from AK47M to AK74M
script.Parent.MouseButton1Click:Connect(function()
local name = script.Parent.Name
local gun = game:GetService("ReplicatedStorage").Storage:FindFirstChild(name)
print(gun.Price.Value)
end)
Anytime, just wanted to help out with better practices as if you plan on adding more guns, you can simply have a single localscript loop through and connect events to every gun button rather than a local script for each as I don’t really see that as good practice, especially if you have to make a change to the buttons.
I recommend you also debug via printing the name variable and the gun variable to see what they return when testing to see if something is going wrong
If you have anymore issues related to this you can shoot me a reply, and if you have any unrelated issues don’t be afraid to make another post!