Making a horror game, tried making a script to make you hold items like you are holding a box in Amnesia. (Take a look)
script.Parent.MouseClick:connect(function(player) local Mouse = player:GetMouse() script.Parent.Parent.Parent = player.Character while true do wait() script.Parent.Parent.CFrame = CFrame.new(Mouse.Hit.X,Mouse.Hit.Y,Mouse.Hit.Z) end end)
When you see it, it looks like this: http://imgur.com/S1tYHgP
When you click it, it twitches a lot and looks like this: http://imgur.com/EyLWDFV
Can anyone help me with this?
In theory, you do not need to put click detectors and the same script in EVERY part or model. You can simply use the HopperBin that ROBLOX has to simulate both not holding a tool, and reaching these goals of moving the object. In fact, people have already done this and created free models, such as the great Malvaviscos. His answer was simple.
Note I do not take credit for this in any way, this scripting is done by Malvaviscos and used in his Force Grip model.
holdRadius = 15 -- The distance from the player that the item is being held. maxSelectRadius = 30 -- The distance that the item can be selected from. local position = Instance.new("BodyPosition") -- A position that is repeatedly set later in the script for movement. position.Name = "Pos" position.maxForce = Vector3.new(100000,100000,100000) local object = nil -- An object that is set as a target later in the script. function onButton1Down(mouse) -- Function activated when a person clicks, called later in the script. if mouse.Target ~= nil then -- If the mouse is hovering over an object, then... if (mouse.Target.Position - game.Players.LocalPlayer.Character.Head.Position).magnitude <= maxSelectRadius and not mouse.Target.Anchored then -- If the target isn't anchored and it's in the range of the Select Radius, then.... object = mouse.Target -- The Target is set as an object. position.Parent = object -- The position's parent is the object target of the mouse. position.position = game.Players.LocalPlayer.Character.Head.Position + (mouse.Hit.p - game.Players.LocalPlayer.Character.Head.Position).unit * holdRadius -- The position value of the BodyPosition from before is set initially. down = true -- The HopperBin is activated and there is a part being held. end end end function onButton1Up(mouse) -- Everything is deselected when the mouse goes back up. object = nil position.Parent = nil down = false end function onMove(mouse) -- If the mouse moves... if down then -- And there is an object, then.... position.position = game.Players.LocalPlayer.Character.Head.Position + (mouse.Hit.p - game.Players.LocalPlayer.Character.Head.Position).unit * holdRadius -- The position is set where the mouse is at a distance of the Hold Radius. end end function onSelected(mouse) -- When the HopperBin is selected as a tool, these three functions will wait for the event to be called, before starting their respective functions. mouse.Button1Down:connect(function() onButton1Down(mouse) end) mouse.Button1Up:connect(function() onButton1Up(mouse) end) mouse.Move:connect(function() onMove(mouse) end) end script.Parent.Selected:connect(onSelected) script.Parent.Deselected:connect(onButton1Up)