Error keeps appearing / Need help with script

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)

RobloxStudioBeta_9UmXJrocib

Print name and see what that brings. IIRC FindFirstChild is case-sensitive, so that might be the problem.

Could you show where this script is located in Explorer, please?

Thanks for replying!
RobloxStudioBeta_meTcZ8Lxw6

I also faced this problem. It can be a spelling mistake.

2 Likes

No problem!
Why don’t use just try adding the actual name of the gun itself?

local Gun = game.ReplicatedStorage.Storage:WaitForChild("AK74M")

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?

You named AK47M and in script you said AK74M

3 Likes

Try editing ur script with this:

local name = script.Parent.Text
local gun = game:GetService("ReplicatedStorage")


script.Parent.MouseButton1Click:Connect(function()
	print(gun.Storage.AK74M.Price.Value)

end)
3 Likes

Still the same error
RobloxStudioBeta_WyVuI90gUj

Change the name of the Gun to “AK74M”

1 Like

Check the Text Property of the button, what did you write there?

1 Like

This worked, thank you, I appreciate it!

glad to be of help :slight_smile:

1 Like

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)
1 Like

I agree.

All the texts are the exact same to their name, I copy and pasted it to make sure of that\

It was originally name = script.Parent.Name

The only reason I changed it was because I thought the .Name was the problem, oviously it was not and I will be changing it back now.

Thank you for replying

1 Like

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!

1 Like