I'm working on a script for my game where when you click on a part it welds itself to you so you can carry it around. I'm (obviously) using a ClickDetector for this but it simply doesn't work. Nothing happens when the part is clicked. I'll assume it's a problem with the ClickDetector since Studio doesn't say there's anything wrong with the script. Anyways, there's probably something wrong with both. This is the LocalScript:
local character = player.Character local humanoid = character:WaitForChild("Humanoid") local Torso = character.Torso local Box = script.Parent.Parent function onClicked(player) print("Clicked by" player.Name) local Weld = Instance.new("WeldConstraint") Weld.Part0 = Torso Weld.Part1 = Box Weld.C1 = CFrame.new (0,1,0) Weld.Parent = Box Box.Anchored = false end) script.Parent.MouseClick:Connect(onClicked)
When clicked it doesn't even print which makes me think there's something wrong with the detector.
Need help making a script to pick up a part when clicked. What's wrong with the script?
One, you might not have reference to player
outside of the function onClicked
in the given context of the code. Therefore trying to reference character
and so on wouldn't work.
Second, you forgot the argument separator (The comma ",") in the arguments for print.
Three, you're using a local script.
For the second problem, you did print("Clicked by" player.Name)
but what you need to do is print("Clicked by", player.Name)
or alternatively print("Clicked by" .. player.Name)
(The ..
means concentration and basically joins two strings together)
Now for the first problem. You will want to define character
, torso
, and humanoid
inside the scope of the function onClicked
For the third problem, simply use a Normal
script rather than LocalScript
or instead change the parent of the LocalScript somewhere replicated to the client.
local Box = ... --// your reference to the box local function onClicked(player) local character = player.Character local torso = character:WaitForChild("Torso") local humanoid = torso:WaitForChild("Humanoid") --// now you create the weld and set its properties. end script.Parent.MouseClick:Connect(onClicked)
The new code (now a normal Script)
local Box = script.Parent.Parent script.Parent.MouseClick:Connect(function(player) local Character = player.Character local torso = Character:WaitForChild("Torso") local humanoid = Character:WaitForChild("Humanoid") print("Clicked by:", player.Name) weld = Instance.new("Weld") weld.Part0 = torso weld.Part1 = Box weld.C1 = CFrame.new (0,1,0) weld.Parent = Box Box.Anchored = false end)