How to give a certain amount of something

How many times is the event being called? That shouldn’t be happening. When the event runs, the Tool variable is only being set to one object, which is then cycled through with the loop, which then ends. I don’t think the problem lies with this script, but rather how the event is being called.

1 Like

Here is the client side script

local UserInputService = game:GetService("UserInputService")
local done = false
local plr = game.Players.LocalPlayer
local leftplayers = {}
local debounce = true


script.Parent.MouseButton1Up:Connect(function()
	debounce = true
	
	local ToolIcon = script.Parent.Parent.Parent:FindFirstChild("ToolIcon")
local TextureId = ToolIcon.Image
local ToolAmount = script.Parent.Parent.TextBox.Text
	
	if ToolIcon then
		
		
	while wait(0.1)  do
		if debounce == true then
			

		print("hit")
		
			for i, hum in pairs(game.Players:GetPlayers()) do
				
					print(hum.Name)
					if hum.Name == plr.Name then
					else
						local otherPlayer = hum
						if otherPlayer then

							local otherCharacter = hum.Character
							local HumPart = otherCharacter.HumanoidRootPart

							if otherPlayer then
								local userId = otherPlayer.UserId
								local thumbType = Enum.ThumbnailType.HeadShot
								local thumbSize = Enum.ThumbnailSize.Size420x420
								local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

								if (HumPart.Position - plr.Character.HumanoidRootPart.Position).Magnitude < 10  then
								 if script.Parent.Parent.Parent.GiveFrame:FindFirstChild(otherPlayer.Name) then
										return
								 else
										local GiveTemp = script.Parent.Parent.Parent.GiveTemplate:Clone()
										GiveTemp.Parent = script.Parent.Parent.Parent.GiveFrame
										GiveTemp.Visible = true
										GiveTemp.ImageFrame.ImageButton.Image = content
										GiveTemp.Name = otherPlayer.Name
								 end
								game.Players.PlayerRemoving:Connect(function(player)
									if player.Name == otherPlayer.Name then
										if script.Parent.Parent.Parent.GiveFrame:FindFirstChild(player.Name) then
											script.Parent.Parent.Parent.GiveFrame:FindFirstChild(player.Name):Destroy()
										end
									end
								end)

									for i, v in pairs(script.Parent.Parent.Parent.GiveFrame:GetChildren()) do
										if v:IsA("Frame") then
											for i,player in pairs(game.Players:GetPlayers()) do
												if v.Name == player.Name then
													v:FindFirstChild("ImageFrame").ImageButton.MouseButton1Click:Connect(function()

														local otherplayer = player
														v:Destroy()
                                                      local UpToolAmount = tonumber(ToolAmount)
														game.ReplicatedStorage.InventoryEvents.GiveEvent:FireServer(otherplayer, TextureId, UpToolAmount)
														debounce = false
													end)
												end

											end
										end
									end



                            wait(3)

								end
							end	
						end
					
				end
				
			end
			else 
				return
		end
	
		
	
	end
	
	end
end)
1 Like

Maybe you are right. I tested it right now and when I had 7 tools and the 3 of them went to the other player

1 Like

You’re looping through multiple items and sending the event for each one. The issue isn’t with the server’s code taking multiple items, this snippet is sending the event again multiple times.

1 Like

How should I modify the script? This looks a bit complex.

1 Like

I made it like this now

	if	script.Parent.Parent.Parent.GiveFrame:FindFirstChild(otherPlayer.Name) then
										script.Parent.Parent.Parent.GiveFrame:FindFirstChild(otherPlayer.Name).ImageFrame.ImageButton.MouseButton1Click:Connect(function()
											local UpToolAmount = tonumber(ToolAmount)
											game.ReplicatedStorage.InventoryEvents.GiveEvent:FireServer(otherPlayer, TextureId, UpToolAmount)
										end)
							end

It gives me 4 now.

local UserInputService = game:GetService("UserInputService")
local done = false
local plr = game.Players.LocalPlayer
local leftplayers = {}
local debounce = true


script.Parent.MouseButton1Up:Connect(function()
	debounce = true
	
	local ToolIcon = script.Parent.Parent.Parent:FindFirstChild("ToolIcon")
local TextureId = ToolIcon.Image
local ToolAmount = script.Parent.Parent.TextBox.Text
	
	if ToolIcon then
		
		
	while wait(0.1)  do
		if debounce == true then
			

		print("hit")
		
			for i, hum in pairs(game.Players:GetPlayers()) do
				
					print(hum.Name)
					if hum.Name == plr.Name then
					else
						local otherPlayer = hum
						if otherPlayer then

							local otherCharacter = hum.Character
							local HumPart = otherCharacter.HumanoidRootPart

							if otherPlayer then
								local userId = otherPlayer.UserId
								local thumbType = Enum.ThumbnailType.HeadShot
								local thumbSize = Enum.ThumbnailSize.Size420x420
								local content, isReady = game.Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

								if (HumPart.Position - plr.Character.HumanoidRootPart.Position).Magnitude < 10  then
								 if script.Parent.Parent.Parent.GiveFrame:FindFirstChild(otherPlayer.Name) then
										return
								 else
										local GiveTemp = script.Parent.Parent.Parent.GiveTemplate:Clone()
										GiveTemp.Parent = script.Parent.Parent.Parent.GiveFrame
										GiveTemp.Visible = true
										GiveTemp.ImageFrame.ImageButton.Image = content
										GiveTemp.Name = otherPlayer.Name
								 end
								game.Players.PlayerRemoving:Connect(function(player)
									if player.Name == otherPlayer.Name then
										if script.Parent.Parent.Parent.GiveFrame:FindFirstChild(player.Name) then
											script.Parent.Parent.Parent.GiveFrame:FindFirstChild(player.Name):Destroy()
										end
									end
								end)
								
								
							if	script.Parent.Parent.Parent.GiveFrame:FindFirstChild(otherPlayer.Name) then
										script.Parent.Parent.Parent.GiveFrame:FindFirstChild(otherPlayer.Name).ImageFrame.ImageButton.MouseButton1Click:Connect(function()
											script.Parent.Parent.Parent.GiveFrame:FindFirstChild(otherPlayer.Name):Destroy()
											local UpToolAmount = tonumber(ToolAmount)
											game.ReplicatedStorage.InventoryEvents.GiveEvent:FireServer(otherPlayer, TextureId, UpToolAmount)
											debounce = false								
										end)
							end

								
                                   


                            wait(3)

								end
							end	
						end
					
				end
				
			end
			else 
				return
		end
	
		
	
	end
	
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.