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!
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)
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)