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

Holding script won't work?

Asked by 8 years ago

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?

0
I'm reposting this because no one is answering. Grenaderade 525 — 8y
0
help me already Grenaderade 525 — 8y
0
can you elaborate on what happens and what's supposed to happen? I don't understand the goal of these pictures. HungryJaffer 1246 — 8y
0
Did you weld the rest of the box to the part that moves? Spectrobz 140 — 8y

1 answer

Log in to vote
1
Answered by
pyro89 50
8 years ago

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)
0
With the above script, you can hold on to any part of the crate as well, unless you wish to rescript it to make the Object the core of the crate or item. pyro89 50 — 8y
0
So how do I use this script? I've never used "HopperBin" Grenaderade 525 — 8y
0
The HopperBin is similar to the Tool on ROBLOX, but it uses different syntax, such as Selected or Deselected instead of Equipped or Unequipped. Put the above in a Script, and put that into a HopperBin. pyro89 50 — 8y
0
Is there anyway you could put in the script where you can rotate it by pressing E + moving your mouse? Grenaderade 525 — 8y
View all comments (2 more)
0
I am sure it is possible, though I am too lazy to be able to help. Sorry about that, though you can start experimenting on your own. pyro89 50 — 8y
0
Well do you know how if you deselect it flies foward a bit? Grenaderade 525 — 8y
Ad

Answer this question