ItemGiver script doesn't work. How could I fix it?

Hello! I’m trying to make a script that gives the player that gets targetted with the mouse if MouseButton2-clicking the item that is currently equipped. It doesn’t work though. I made some prints to make it easier to fix the issue, I don’t really know why it is not working though.

local player = game.Players.LocalPlayer
local Tool = script.Parent
local mouse = player:GetMouse()
local Character = player.CharacterAdded:Wait()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local isEquipped = false

Tool.Equipped:Connect(function()
	isEquipped = true
end)

Tool.Unequipped:Connect(function()
	isEquipped = false
end)


mouse.Button2Down:Connect(function()
	
	print("Yes1") --Gets printed
	
	if isEquipped == true then
		
		print("Yes2") --Gets printed
		
		local MouseTarget = mouse.Target
		
		if MouseTarget ~= nil then
			
			print("Yes3") --Gets printed
			
			if MouseTarget:FindFirstChild("Humanoid") then
				
				print("Yes4") --Doesn't get printed
				
				local TargetPlayer = game.Players:GetPlayerFromCharacter(MouseTarget)
				
				if TargetPlayer ~= game.Players:GetPlayerFromCharacter(Character) then
					
					print("Yes5") --Doesn't get printed
					
					local TargetPlayerBackpack = TargetPlayer.Backpack
					
					local ClonedTool = ReplicatedStorage.Tool:Clone()
					ClonedTool.Parent = TargetPlayerBackpack
					
					print("Success!") --Doesn't get printed

				end		
				
			end
			end
	end
end)```

From what I can tell you’re doing this on a local script. You’ll need to handle the tool giving part of the script on a server script as a local script will only affect the client of the player holding the tool. I would recommend using a RemoteEvent to pass on the information to a server script which handles the Cloning of the tool and gives it to the target player.

Yes4 should get printed though? It means that it can’t find a Humanoid, which is pretty weird. I tested it on a TestServer and it didn’t work.

Use this instead for finding the character:

If MouseTarget.Parent:FindFirstChild('Humanoid') then
local TargetPlayer = game.Players:GetPlayerFromCharacter(MouseTarget.Parent)
1 Like

Yep, it works, now. It even printed Success once, lol. I will need a RemoteEvent. I don’t know how to pass over information though. ;-;

To use a remote event place one in ReplicatedStorage and use this kind of code:

--- Local script
local TargetPlayer = (put the player you want to give the tool too here)
local Tool = 'Tool name here'
game.ReplicatedStorage.RemoteEvent:FireServer(TargetPlayer,Tool)

---- Server script

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, TargetPlayer, Tool)
     local ToClone = game.ServerStorage:FindFirstChild(Tool):Clone()
     ToClone.Parent = TargetPlayer.Backpack
end)

You can read more about it here:

2 Likes

Thank you so much! The first parameter of the OnServerEvent is always the player who fired the RemoteEvent, right?

1 Like

It still doesn’t work. Yes4 and Yes5 don’t get printed.

--Local Script:
local player = game.Players.LocalPlayer
local Tool = script.Parent
local mouse = player:GetMouse()
local Character = player.CharacterAdded:Wait()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ItemGive = ReplicatedStorage:FindFirstChild("ItemGive")
local isEquipped = false

Tool.Equipped:Connect(function()
	isEquipped = true
end)

Tool.Unequipped:Connect(function()
	isEquipped = false
end)


mouse.Button2Down:Connect(function()
	
	print("Yes1") --Gets printed
	
	if isEquipped == true then
		
		print("Yes2") --Gets printed
		
		local MouseTarget = mouse.Target
		
		if MouseTarget ~= nil then
			
			print("Yes3") --Gets printed
			
			if MouseTarget.Parent:FindFirstChild("Humanoid") then
				
				print("Yes4") --Doesn't get printed
				
				local TargetPlayer = game.Players:GetPlayerFromCharacter(MouseTarget.Parent)
				
				if TargetPlayer ~= game.Players:GetPlayerFromCharacter(Character) then
					
					print("Yes5") --Doesn't get printed
					
					local TargetPlayerBackpack = TargetPlayer.Backpack
					
					ItemGive:FireServer(TargetPlayerBackpack, Tool)
					
					print("Success!") --Doesn't get printed

				end		
				
			end
			end
	end
end)


--ServerScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ItemGive = ReplicatedStorage:FindFirstChild("ItemGive")
local ToolToGetCloned = ReplicatedStorage:FindFirstChild("Tool")


ItemGive.OnServerEvent:Connect(function(plr, TargetPlayerBackpack)
	local ToolClone = ToolToGetCloned:Clone()
	ToolClone.Parent = TargetPlayerBackpack
	
end)```

Try removing .Parent and see if that works.

Nope, that doesn’t work aswell. ;-;

Does it give any errors in the output window?

Nope, it doesn’t. It just prints Yes1, Yes2 and Yes3.

Alright, I don’t care about it anymore, I just wanted to make it for fun and for practice but seems like it got really complicated and hard to fix. Thank you!

You could make an else statement after if MouseTarget.Parent:FindFirstChild("Humanoid") then and add print(MouseTarget.Parent) to debug what the mouse target’s parent is.

if MouseTarget.Parent:FindFirstChild("Humanoid") then
				
				print("Yes4") --Doesn't get printed
else
                                print(MouseTarget.Parent) --Prints the MouseTarget's parent
1 Like

My output:
Yes1 (x3)

Yes2

Yes3

Finalized Wings v2

Yes5

21:23:10.012 - Players.Player1.Backpack.Tool.LocalScript:44: attempt to index nil with ‘Backpack’

21:23:10.013 - Stack Begin

21:23:10.014 - Script ‘Players.Player1.Backpack.Tool.LocalScript’, Line 44

21:23:10.014 - Stack End

My script:

local player = game.Players.LocalPlayer
local Tool = script.Parent
local mouse = player:GetMouse()
local Character = player.CharacterAdded:Wait()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ItemGive = ReplicatedStorage:FindFirstChild("ItemGive")
local isEquipped = false

Tool.Equipped:Connect(function()
	isEquipped = true
end)

Tool.Unequipped:Connect(function()
	isEquipped = false
end)


mouse.Button2Down:Connect(function()
	
	print("Yes1") --Gets printed
	
	if isEquipped == true then
		
		print("Yes2") --Gets printed
		
		local MouseTarget = mouse.Target
		
		if MouseTarget ~= nil then
			
			print("Yes3") --Gets printed
			
			if MouseTarget.Parent:FindFirstChild("Humanoid") then
				
				print("Yes4") --Doesn't get printed
			else
                print(MouseTarget.Parent)  --Doesn't get printed
				
				local TargetPlayer = game.Players:GetPlayerFromCharacter(MouseTarget.Parent)
				
				if TargetPlayer ~= game.Players:GetPlayerFromCharacter(Character) then
					
					print("Yes5") --Doesn't get printed
					
					local TargetPlayerBackpack = TargetPlayer.Backpack
					
					ItemGive:FireServer(TargetPlayerBackpack, Tool)
					
					print("Success!") --Doesn't get printed

				end		
				
			end
			end
	end
end)


Looks like there is something wrong with this line (Line 44)

	local TargetPlayerBackpack = TargetPlayer.Backpack```

Alright, I gave up on it. Thank you for trying to help me.