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

Why Wont This Script Work?

local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent
local playercharacter = player.Character
local playerid = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.userId
local money = player:FindFirstChild("Money")
local price = script.Parent.Price.Value
local text = script.Parent.Text
local amo = script.Parent.Ammo.Value
local gun  = player.Backpack:FindFirstChild("Peacekeeper") or playercharacter:FindFirstChild("Peacekeeper")
script.Parent.MouseButton1Click:connect(function()
	if money.Value >= price then
		gun.StoredAmmo.Value = gun.StoredAmmo.Value + 40
			money.Value = money.Value - price
		elseif money.Value < price then
			script.Parent.Text = "You Dont Have Enough Money!"
			wait(2)
			script.Parent.Text = text
	end
end)

this script is in startergui when the game starts it moves to playergui. in the out put this error shows 23:26:57.933 - Players.Player1.PlayerGui.ShopGui.Background.AmmoFrame.PeacekeeperAmmo.Revolver Ammo 40.Script:12: attempt to index upvalue 'gun' (a nil value) 23:26:57.933 - Stack Begin 23:26:57.934 - Script 'Players.Player1.PlayerGui.ShopGui.Background.AmmoFrame.PeacekeeperAmmo.Revolver Ammo 40.Script', Line 12 please help someone

1 comment

0

May you fix your formatting and explain more about what your trying to do

1 answer

On line 12 (Line 11 in the paste) you write

gun.StoredAmmo.Value = gun.StoredAmmo.Value + 40

Where gun is from

local gun  = player.Backpack:FindFirstChild("Peacekeeper") or playercharacter:FindFirstChild("Peacekeeper")

However, gun is not certainly existing, and may become nil if the Player dies. The solution is to move the line defining gun into the event connection and then add a nil check.

1 comment

0

i did show where the gun was on line 8 but it still wont work

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