So basically, I’m tryna make a survival rpg type game, and I’m working on the mining system. I’m trying to use tables to select which pickaxes can mine which ore. I’ve tried pairs but I probably scripted it wrong, I got quite tired trying to script it, so my current script is quite lazy.
Anyways, here’s the script:
local SPT = script.Parent
local SPM = script.Parent.StonePickaxe
local strike = script.Parent.Strike
local plr = game.Players.LocalPlayer
local chr = plr.Character
local mouse = plr:GetMouse()
local hmd = chr:WaitForChild("Humanoid")
local stoneTable = {
"stone1",
"stone2",
"stone3",
}
SPT.Activated:Connect(function()
if mouse.Target.Name == stoneTable then
strike:Play()
end
end)
I always shorten variables when they are multiple words long, so here’s what my variables mean:
“SPT” = “Stone Pickaxe Tool”
“SPM” = “Stone Pickaxe Model”
“strike” = the sound I used for the pickaxe.
I feel like the other variables are quite self explanatory.
If anyone can help at all, I would greatly appreciate it. Thank you!
This is the problem, you are trying to compare a string with a table. Maybe try using something like if table.Find(stoneTable, mouse.Target.Name) then.
You could assign the mouse.Target a tag to the type of the object, using this type you can get it from a “resource table” then compare based on what the pickaxe value is
So, the issue with your script is that you’re trying to check if mouse.Target.Name is equal to the whole stoneTable (which is a table, not a single string). What you need to do is check if the mouse.Target.Name is inside the table. You can do this by making a function that loops through the table and checks if any of the stone names match the target’s name. Here’s how you can fix it: Instead of comparing mouse.Target.Name directly to stoneTable, you make a function like isStone that checks if the target’s name is in the table. That way, you can easily add new stones to the table and the script will handle it without much hassle. Here’s an example of how to do it:
local stoneTable = {
"stone1",
"stone2",
"stone3",
}
local function isStone(targetName)
for _, stone in ipairs(stoneTable) do
if targetName == stone then
return true
end
end
return false
end
SPT.Activated:Connect(function()
if mouse.Target and isStone(mouse.Target.Name) then
strike:Play()
end
end)
Now the script will check if the mouse.Target.Name is in the stoneTable, and if it is, it’ll play the strike sound. You can easily add new stones to the table as your game grows, and it’ll keep working without needing much editing.
you are checking if [mouse.Target.Name] is set to true, this would work for dictionaries where the value is set to true but not for regular tables, you have to check if it exists by using
if table.find(stoneTable, mouse.Target.Name) then
strike:play()
end
yeah I knew that I somehow had to get the strings inside of the table, but that’s where I was having more trouble, and I was quite lazy and tired when I was making the script so I just kinda threw in the table name. But thank you very much for your help, I’ll try this out now!
So I tried the script out, unfortunately, it didn’t work, I tried editing it a little, and it still didn’t work. So now, I’m trying out a different idea. I’m gonna try and use clickdetectors. My idea is that, I’m going to use FindFirstChild or WaitForChild to find if the mouses target contains a clickdetector, and if it does, then if the player left clicks, it’s gonna try and do the stuff.
I’m taking a little break from the mining system though, I’m currently working on the cursors, I’m making decent progress so far. But anyways, I’ll update you if it works. Thank you!
Those variables names aren’t long. They’re standard. It is extremely counterproductive to simplify variables down to non-standard acronyms—it wildly affects readability. I recommend reading the official Roblox style guide as well as watching this video on objective positive readability practices.
On another note, it’s not efficient to attempt to store every title of ore a particular piece of mining equipment can extract. Instead, classify your ore via attributes, then match the mouse’s target to that type. If your equipment can mine more than one type of ore, then return to using tables. It is as simple as:
local minableOres = {
"Stone",
"Coal",
"Iron",
}
local function isMinableOre(oreType: string): boolean
return table.find(minableOres, oreType)
end
You could alternatively assign weights to ores, which would enable you to develop tools that work on procurement difficulty or ore strength. The particular piece of mining equipment would have a maximum operarting procurement difficulty or ore strength limit. This approach makes the most sense, as having developing tools for specific ores is too tedious, especially for usage by players