How to give a certain amount of something

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)
4 Likes
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)

Something like that?

6 Likes

I tried something like that before but because there is a for loop it gives them all at once should I use break?

1 Like

Can you explain better please? I dont really understand what you mean sorry.

1 Like

Yes, because there is a loop there if we have for example 3 same guns it gives them all

2 Likes

Anyways, I will try and tell you

2 Likes

So if you already for example have a specific a gun and it finds the same gun in inventory you need to not add it or what?

2 Likes

Do you know how to convert a string into a number?

1 Like

tonumber(your string here) its pretty simple

3 Likes

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.

2 Likes

Yes.; That’s what I am trying to do

2 Likes

The way I would do this is by following essentially the same approach that @0PaperCube0 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)

Edit: Bad whitespace by me.

2 Likes

I want to give it the player as much tools as the toolamount is

1 Like

I am going to try your script anyways.

1 Like

I tried it but the player got 2 tools despite the toolamount being one.

1 Like

Hmm, interesting. Try replacing this snippet:

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.

2 Likes

Still doesn’t is it because of the loop? Because if there are for example 2 tools the loop maybe make changes to all of them

1 Like

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.

1 Like

Isn’t that script the same as before?

1 Like

It still gives to the player all of the tools which are the same type

1 Like