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

Attempt To Index Global 'price' (a number value)!?

Ok i'm trying to do a shop script, everything is fine until the part where u need to buy the knife/weapon, Here is the code

Local Script Code (Gui):

local plr = game:GetService("Players").LocalPlayer
local VF = plr:WaitForChild("ValuesFolder")
local Coins = VF:WaitForChild("Coins")
local Diamonds = VF:WaitForChild("Diamonds")
local WeaponName = "Knife"
local WeaponType = "Melee" --//Currenty Types: Melee , Firearm
local ValueToBeOwned = "HasKnife"
local VTBO = VF[ValueToBeOwned]
local Price = 0
local PriceV = 0
local debounce = false
local gui = script.Parent

script.Parent.MouseButton1Click:Connect(function()
	if not debounce then
		debounce = true
		local noob = plr.Backpack:FindFirstChild(WeaponName)
		if Coins.Value >= Price and Diamonds.Value >= PriceV and 						  not noob then
			game.ReplicatedStorage.Remotes.BuyItem:FireServer(WeaponName,Pr		ice,PriceV,WeaponType,ValueToBeOwned)
			if Price.Value <= 0 then
				gui.Text = "Gotcha!"
				wait(1)
				gui.Text = "Get"
			else
				gui.Text = "Purchased!"
				wait(1)
				gui.Text = "Buy"
			end
		elseif Coins.Value < Price and Diamonds.Value < PriceV then
			gui.Text = "Not Enough Coins/Diamonds :("
			wait(1)
			if Price.Value <= 0 then
				gui.Text = "Get"
			else
				gui.Text = "Buy"
			end
		elseif VTBO.Value == true then
			gui.Text = "Already Owned! :D"
			wait(1)
			if Price.Value <= 0 then
				gui.Text = "Get"
			else
				gui.Text = "Buy"
			end
		elseif noob then
			gui.Text = "U Already Has One With You! >:C"
			wait(1)
			if Price.Value <= 0 then
				gui.Text = "Get"
			else
				gui.Text = "Buy"
			end
		end
		debounce = false
	end
end)

Remote Event Script (Fired By Local Script):

game.ReplicatedStorage.Remotes.BuyItem.OnServerEvent:Connect(function(plr,ItemName,P,P2,WT,VTBO)
	local VF = plr:WaitForChild("ValuesFolder")
	local Coins = VF:WaitForChild("Coins")
	local Diamonds = VF:WaitForChild("Diamonds")
	local MeleeFolder = game.ReplicatedStorage.Weapons:FindFirstChild(WT)
	local Weapon = MeleeFolder[ItemName]
	local noob = plr.Backpack:FindFirstChild(ItemName)
	local Value = VF[VTBO]
	if MeleeFolder and Weapon and not noob then
		if Value.Value == false then
			Coins.Value = Coins.Value - P
			Diamonds.Value = Diamonds.Value - P2
			Weapon:Clone().Parent = plr.Backpack
		end
	end
end)

[SerpentineKing]: Input Proper Code Format / Indentation

1 answer

✓ Accepted answer

General Practice

Use :GetService() to retrieve the Replicated Storage

Use Activated rather than MouseButton1Click since Activated works on all devices and not just computers

Issues

When you intially define Price you state

local Price = 0

However, you later state multiple lines using Price.Value, when Price was clearly defined as 0 initially. So either Price in those situations was meant to be a different variable such as Coins, or if they are meant to be there, then you need to remove the .Value from each line that states Price.Value

Revised Local Script

local plr = game:GetService("Players").LocalPlayer
local VF = plr:WaitForChild("ValuesFolder")
local Coins = VF:WaitForChild("Coins")
local Diamonds = VF:WaitForChild("Diamonds")
local WeaponName = "Knife"
local WeaponType = "Melee" --//Currenty Types: Melee , Firearm
local ValueToBeOwned = "HasKnife"
local VTBO = VF[ValueToBeOwned]
local Price = 0
local PriceV = 0
local debounce = false
local gui = script.Parent

script.Parent.Activated:Connect(function()
	if not debounce then
		debounce = true
		local noob = plr:WaitForChild("Backpack"):FindFirstChild(WeaponName)
		if Coins.Value >= Price and Diamonds.Value >= PriceV and 						  not noob then
			game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("BuyItem"):FireServer(WeaponName,Pr		ice,PriceV,WeaponType,ValueToBeOwned)
			if Price <= 0 then
				gui.Text = "Gotcha!"
				wait(1)
				gui.Text = "Get"
			else
				gui.Text = "Purchased!"
				wait(1)
				gui.Text = "Buy"
			end
		elseif Coins.Value < Price and Diamonds.Value < PriceV then
			gui.Text = "Not Enough Coins/Diamonds :("
			wait(1)
			if Price <= 0 then
				gui.Text = "Get"
			else
				gui.Text = "Buy"
			end
		elseif VTBO.Value == true then
			gui.Text = "Already Owned! :D"
			wait(1)
			if Price <= 0 then
				gui.Text = "Get"
			else
				gui.Text = "Buy"
			end
		elseif noob then
			gui.Text = "U Already Has One With You! >:C"
			wait(1)
			if Price <= 0 then
				gui.Text = "Get"
			else
				gui.Text = "Buy"
			end
		end
		debounce = false
	end
end)

Revised Server Script

game:GetService("ReplicatedStorage"):WaitForChild("Remotes"):WaitForChild("BuyItem").OnServerEvent:Connect(function(plr,ItemName,P,P2,WT,VTBO)
		local VF = plr:WaitForChild("ValuesFolder")
		local Coins = VF:WaitForChild("Coins")
		local Diamonds = VF:WaitForChild("Diamonds")
		local MeleeFolder = game:GetService("ReplicatedStorage"):WaitForChild("Weapons"):FindFirstChild(WT)
		local Weapon = MeleeFolder[ItemName]
		local noob = plr:WaitForChild("Backpack"):FindFirstChild(ItemName)
		local Value = VF[VTBO]
		if MeleeFolder and Weapon and not noob then
			if Value.Value == false then
				Coins.Value = Coins.Value - P
				Diamonds.Value = Diamonds.Value - P2
				Weapon:Clone().Parent = plr:WaitForChild("Backpack")
			end
		end
	end)

When the button is clicked, if each weapon has a set price, you can set the Price variable to equal that number value once the button is clicked.

1 comment

0

thx it worked :D

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