Why is this not working?

Hello, I have this script for Purchasing tools it should print the price of the tool but it’s printing nil

Capture

the code

Client

local Player = game:GetService("Players").LocalPlayer
local Button = script.Parent
local ToolName = script.Parent.Name
Button.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Events.PurchaseTool:FireServer(ToolName)
end)

Server

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseTool = ReplicatedStorage.Events:WaitForChild("PurchaseTool")

local ToolPrices = {
	Tool1 = 5000,
}

PurchaseTool.OnServerEvent:Connect(function(Player, Tool)
	local ToolName = Tool.Name
	local ToolPrice = ToolPrices.ToolName
	print(ToolPrice)
	--more code
end)	

Huh i had the same problem, don’t pass an instance from the client to the server, instead, search it inside the server. like:

local Tool = player.Backpack:FindFirstChild("Tool") -- this is inside the server
1 Like

Oh wait, in the tool price variable aren’t you searching for the toolname? for that you need to use

local ToolPrice = ToolPrices:FindFirstChild(ToolName)
1 Like

now I get the error attempt to call a nil value

Capture

code

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseTool = ReplicatedStorage.Events:WaitForChild("PurchaseTool")

local ToolPrices = {
	Tool1 = 5000,
}

PurchaseTool.OnServerEvent:Connect(function(Player, Tool)
	local ToolName = Tool.Name
	local ToolPrice = ToolPrices:FindFirstChild(ToolName)
	print(ToolPrice)
	--more code
end)

Can you show me the explorer in that part please?

1 Like

Uhhh

Capture

No, i mean the ToolPrice childrens

1 Like

You need brackets for this.

local ToolPrice = ToolPrices[ToolName]

As an explanation, ToolName is not a name of a value in the table, but rather a whole other variable itself, so you need to use brackets.

1 Like

Turn the index within the table into a string and then reference it as such.

ToolPrices = {
[“Tool1”] = 5000
}

ToolPrices[“Tool1”] => 5000

1 Like

The table itself is fine as it is, so I have no idea why you are correcting it. It works fine without brackets or quotation marks.

ToolPrices = {
    Tool1 = 5000
}

print(ToolPrices.Tool1) --> 5000
1 Like

He wants to automate the process and find the ToolPrice for any given ToolName. Therefore, if the ToolName argument is passed to the function, he can use it as an index within the table which is what I have presented.

1 Like

Yes, but the thing is, it is already an index within the table, so it needs no correcting.

1 Like

I see what you were saying. I was confused and thought that you meant something else.

I suppose it’s just a matter of formatting preference, then.

2 Likes

with ToolPrices[ToolName] it still prints nil
Capture

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseTool = ReplicatedStorage.Events:WaitForChild("PurchaseTool")

local ToolPrices = {
	Tool1 = 5000
}

PurchaseTool.OnServerEvent:Connect(function(Player, Tool)
	local ToolName = Tool.Name
	local ToolPrice = ToolPrices[ToolName]
	print(ToolPrice)
	--more code
end)

That’s quite weird. Try printing the tool name and see what pops up. Could be some sort of mess up with the passing.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseTool = ReplicatedStorage.Events:WaitForChild("PurchaseTool")

local ToolPrices = {
	Tool1 = 5000
}

PurchaseTool.OnServerEvent:Connect(function(Player, Tool)
	local ToolName = Tool.Name
	print(ToolName)
	local ToolPrice = ToolPrices[ToolName]
	print(ToolPrice)
	--more code
end)
1 Like

It prints nil for the tool name

Capture

That would be the problem here. Can I see what you’re passing from the client?

1 Like

Client

local Player = game:GetService("Players").LocalPlayer
local Button = script.Parent
local ToolName = script.Parent.Name
Button.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Events.PurchaseTool:FireServer(ToolName)
end)

You pass the tool name directly, so have it like this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseTool = ReplicatedStorage.Events:WaitForChild("PurchaseTool")

local ToolPrices = {
	Tool1 = 5000
}

PurchaseTool.OnServerEvent:Connect(function(Player, ToolName)
	local ToolPrice = ToolPrices[ToolName]
	print(ToolPrice)
	--more code
end)
1 Like