Give a tool when a part is touched

  1. What I want to achieve?
    I want to give the player who touched a model a tool
local part = script.Parent.Handle
local repl = game:GetService("ReplicatedStorage")
local tool = repl.fruits.Tool.Gum_bum
part.Touched:Connect(function(Object)
	if Object.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players[Object.Parent.Name]
		tool:Clone()
		tool.Parent = Player.Backpack
	end
end)

2 Likes

So this piece of code used to work back in the day we didn’t necessarily needed to use remote events. But we do really need to use them now.

This will give the player the tool you want him to have but only locally. In order to give the tool to the player locally and server sided whe shall need to use remote events.

Local script:

local part = script.parent.Handle
local repl = game:GetService("ReplicatedStorage")

part.Touched:Connect(function(object)
   if Object.Parent:FindFirstChild("Humanoid") then
      repl.YourRemoteEvent:FireServer()
   end
end)

Server script:

game.ReplicatedStorage.YourRemoteEvent.OnServerEvent:Connect(function(Player)
    tool = path.to.tool
    tool:Clone()
    tool.parent = Player.Backpack
end)

I haven’t tested this code yet because I have a lack of time, but this should get you into the right direction. Hope this helps!

3 Likes

I will be testing it now and reply with the results

I hade to change some of your code but it works perfectly

2 Likes

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