Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Need help making a script to pick up a part when clicked. What's wrong with the script?

Asked by 4 years ago

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.

0
It's silly but you have an error in "print" xD PepeElToro41 132 — 4y
0
Lol Emilius4477 2 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Question

Need help making a script to pick up a part when clicked. What's wrong with the script?

What is and may be wrong.

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.

Solutions

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.

Example code for Problem 1

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)

Suggested Articles

http://lua-users.org/wiki/ScopeTutorial

https://www.tutorialspoint.com/lua/lua_functions

0
I took your suggestion and down below I'll place the new code. The box still doesn't move and I see an error reading "22:44:01.222 - Infinite yield possible on 'Workspace.Emilius4477:WaitForChild("Torso")" Emilius4477 2 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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)
0
The character might be r15. Try replacing `("Torso")` with `("UpperTorso")` EpicMetatableMoment 1444 — 4y
0
It works now! Thanks for the help! Emilius4477 2 — 4y
0
Glad I could help! EpicMetatableMoment 1444 — 4y

Answer this question