I want to make it so it gives as much same type of tools as the toolAmount Value, but I don’t know where to start? Please help me.
game.ReplicatedStorage.InventoryEvents.GiveEvent.OnServerEvent:Connect(function(plr,otherplayer,TextureId, ToolAmount)
local Tool
for i,v in pairs(plr:GetDescendants()) do
if v:IsA("Tool") then
if v.TextureId == TextureId then
Tool = v
end
end
end
local NewTool = Tool:Clone()
NewTool.Parent = otherplayer.Backpack
Tool:Destroy()
end)
game.ReplicatedStorage.InventoryEvents.GiveEvent.OnServerEvent:Connect(function(plr,otherplayer,TextureId, ToolAmount)
local Tool
for i,v in pairs(plr:GetDescendants()) do
if v:IsA("Tool") then
if v.TextureId == TextureId then
Tool = v
end
end
end
local counter = ToolAmount
while counter ~= 0 do
counter -= 1
local NewTool = Tool:Clone()
NewTool.Parent = otherplayer.Backpack
Tool:Destroy()
wait()
end
end)
Could you provide just a bit more information as to what you’re trying to do? I think what you’re asking is for a way to add a certain tool a specific number of times to the player’s inventory, right? I’m not quite sure.
The way I would do this is by following essentially the same approach that @IceedCubee recommended. Though what exactly do you mean by this?
Yes, because there is a loop there if we have for example 3 same guns it gives them all
I THINK what you mean is that you only want to cycle through each tool once. So, if you give someone Tool1 3 times, they get it 3 times and it’s removed from your inventory. But if you have another Tool1, then it’ll do it again? In that case, what I would do is assign a variable at the beginning of the function, and then set it to true when we find the correct tool. Something like this:
game.ReplicatedStorage.InventoryEvents.GiveEvent.OnServerEvent:Connect(function(plr,otherplayer,TextureId, ToolAmount)
local Tool
local FoundTool = false -- the variable we create to be changed later
for i,v in pairs(plr:GetDescendants()) do
if FoundTool then break end -- will break the loop if FoundTool == true
if v:IsA("Tool") then
if v.TextureId == TextureId then
FoundTool = true -- we've found the correct tool and set the variable to true so that we won't look for it again
Tool = v
end
end
end
local counter = ToolAmount
while counter ~= 0 do
counter -= 1
local NewTool = Tool:Clone()
NewTool.Parent = otherplayer.Backpack
Tool:Destroy()
wait()
end
end)
local counter = ToolAmount
while counter ~= 0 do
counter -= 1
local NewTool = Tool:Clone()
NewTool.Parent = otherplayer.Backpack
Tool:Destroy()
wait()
end
With this instead.
local counter = 0
if ToolAmount == 0 then return end
repeat
counter += 1
local NewTool = Tool:Clone()
NewTool.Parent = otherplayer.Backpack
until counter == ToolAmount
Tool:Destroy()
Edit: Made a small change to prevent a crash in case ToolAmount is set to 0.
No, it won’t. And the change I suggested earlier about the FoundTool variable was actually pointless because of how I said to implement it. But no, and that’s because of this loop here:
for i,v in pairs(plr:GetDescendants()) do
if v:IsA("Tool") then
if v.TextureId == TextureId then
Tool = v
end
end
end
This loop will only return one tool for us to use. This means that we will only do this cycle for one tool per event call.
Also, you should probably look into methods to secure your remotes. What I’m seeing should function fine, but you should think of various ways exploiters might abuse your remotes to take advantage of how your script is configured.