4667hp
(Account Info)
May 25, 2022, 12:49am
#1
Hello, I have this script for Purchasing tools it should print the price of the tool but it’s printing nil
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)
elrayocraft8
(FloridaFanAccountOwO)
May 25, 2022, 12:51am
#2
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
elrayocraft8
(FloridaFanAccountOwO)
May 25, 2022, 12:53am
#3
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
4667hp
(Account Info)
May 25, 2022, 12:56am
#4
now I get the error attempt to call a nil value
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)
elrayocraft8
(FloridaFanAccountOwO)
May 25, 2022, 12:57am
#5
Can you show me the explorer in that part please?
1 Like
No, i mean the ToolPrice childrens
1 Like
Kaid3n22
(Kaiden)
May 25, 2022, 1:06am
#8
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
Kaid3n22
(Kaiden)
May 25, 2022, 1:10am
#10
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
Kaid3n22
(Kaiden)
May 25, 2022, 1:12am
#12
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
4667hp
(Account Info)
May 25, 2022, 1:17am
#14
with ToolPrices[ToolName] it still prints nil
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)
Kaid3n22
(Kaiden)
May 25, 2022, 1:24am
#15
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
4667hp
(Account Info)
May 25, 2022, 1:26am
#16
It prints nil for the tool name
Kaid3n22
(Kaiden)
May 25, 2022, 1:27am
#17
That would be the problem here. Can I see what you’re passing from the client?
1 Like
4667hp
(Account Info)
May 25, 2022, 1:28am
#18
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)
Kaid3n22
(Kaiden)
May 25, 2022, 1:30am
#19
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