Give player tool when they touch a part

Hey there, I am trying to figure out how to give a player a tool if they touch a part and if they have a tool with a specific name.

This is what I have so far:

	if (h.Parent.Name == "Large Chocolate Milk") then
		local Tool = game.ServerStorage.Milkshakes.Chocolate["Large Chocolate Milkshake"]:Clone()
		Tool.Parent = h.Parent.Backpack
	end
script.Parent.Touched:Connect(touch)

I don’t know if I am just being stupid, but I can’t figure this out. Any help will be greatly appreciated!

Show full script please. Also you should reference the milk shake out side of this supposed event listener.

local serverStorage = game:GetService("ServerStorage")
local playersService = game:GetService("Players")
local debounces = {}

local function OnTouched(Object)
	if not Object.Parent:FindFirstChild("Humanoid") and not playersService:GetPlayerFromCharacter(Object.Parent) then return end
	local player = playersService:GetPlayerFromCharacter(Object.Parent)
	debounces[player.Name] = false
	if debounces[player.Name] then return end
	debounces[player.Name] = true
	local tool = serverStorage["ToolName"]:Clone()
	if not player:FindFirstChild(tool.Name) then
		tool.Parent = player.Backpack
		debounces[player.Name] = false
	end
end

script.Parent.Touched:Connect(OnTouched)
1 Like

You need to actually connect the event to a function.

New code:

--//Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

--//Variables
local Part = script.Parent
local MilkShake = ServerStorage.Milkshakes.Chocolate:WaitForChild("Large Chocolate Milkshake")

--//Functions
Part.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	
	if not Player then
		return
	end
	
	local newTool = MilkShake:Clone()
	newTool.Parent = Player.Backpack
end)
3 Likes

Thank you! This worked well.

char

No problem. If you have any more questions, feel free to ask.

1 Like