As the title indicates , i’ve been trying to give players specific weapons per key in a table , script in severscriptservice, like :
local players = game:GetService("Players")
local player = players.LocalPlayer
local rs = game.ReplicatedStorage
local t = player.folder2.toolnumber
local x = t.Value--for example 1
--table new to convert 1 to text for tooldictionary
local numberconv = {
1=="one";
2=="two";
3=="three";
4=="four";
--etc.
}
local toolkey = table.find(numberconv,nil,x)
toolsdictionary = {
one = rs.tool2;
two = rs.Sword;
three = rs.tool3;
}-- or ]]
key_result = table.find(toolsdictionary,toolkey,x)
for key, value in pairs(toolsdictionary) do
local r = key_result:Clone()
r.Parent = player.Backpack
end
–tools are in replicated storage, the sword for example.
I think there’s some unncessary iteration of some sort but I can’t seem to get it.
What could I be doing wrong , can anyone please explain the cause and/or a possible fix? Also should I be doing all this in a module script , if yes, then how?
(Seen Dev Hub and other sources, not very helpful, other than with the basics)
Firstly please format your code better so it’s easier to assist you in the future. Feel free to use my guide if you need help with how to do that.
To answer your questions, ModuleScripts are only run once they are required by some other script. Your numberconv table also looks a bit iffy as well as your toolsdictionary. You have a bit of redundancy here unless you need to do this for some other purpose. You can do this to shorten down on your code
-- can be placed in a ModuleScript if you want
local tools = {
[1] = rs.tool2;
[2] = rs.Sword;
-- etc
}
If the user is selecting a number on their GUI you would need to use a RemoteFunction (return with whether they receive the tool or not from server) which would be called every time the user selects a number (be sure to use the tonumber() function). So you would invoke the RemoteFunction from the client and on the server you would take the number given by the client and run whichever checks you need to run. Afterward, you would do something like
local tool = tools[NUMBER_FROM_CLIENT]:Clone()
tool.Parent= player.Backpack