i would like to find some errors in my code and hope to achieve what i need in the title.
local part = Instance.new("Part")
local tool = Instance.new("Tool")
local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = part
local function onClick()
part.Parent = tool
tool.Parent = game.Players.LocalPlayer.Backpack
end
part.clickDetector.MouseClick:Connect(onClick)
one problem i face is that it doesnt register the Local Player, and returns an error with a nil index. i dont know how to fix that, neither i know whether my code is actually correct. can someone help me out?
you cant call LocalPlayer directly using game.players.localplayer from server, use the function
---//Parts
local PartToClick = script.Parent
-- //Tool
local Tool = Instance.new("Tool", PartToClick)
local HandlePart = Instance.new("Part", Tool)
local ClickDetector = Instance.new("ClickDetector", PartToClick)
function onMouseClick(player: Player)
-- //Backpack
local Backpack = player.Backpack
-- //Parent
if Tool.Parent == PartToClick then
Tool.Parent = Backpack
else
warn("Tool Parent:", Tool.Parent)
end
end
ClickDetector.MouseClick:Connect(onMouseClick)