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

Moving a model in front of my torso?

Asked by 5 years ago

Okay, so I want to move a model in front of my torso, but that's not working though :P

  • Yes I know KeyDown is deprecated, just help me, please. *
local m = game.Players.LocalPlayer:GetMouse()
local character = game.Players.LocalPlayer.Character
local Torso = game.Players.LocalPlayer.Character.UpperTorso
local active = false
local IceShield = script.IceShield
local position = Torso.Position + Torso.CFrame.lookVector * 5

db = true
m.KeyDown:connect(function(k)
k = k:lower()
if k == 'x' then
    IceShield:Clone().Parent = game.Workspace
    IceShield:MoveTo(position)
end
end)
0
Fixed User#19524 175 — 5y

2 answers

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

You shouldn't use MoveTo to move a model, it can cause weird behaviour, such as teleporting to the sky. Set a PrimaryPart on your model and use SetPrimaryPartCFrame. Also, you're recording the position before the key is pressed, so the first position is when the character first spawns. Record the position in the event.

Mouse.KeyDown and connect are deprecated, switch to UserInputService and Connect instead.

local UserInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Torso = character.UpperTorso
local active = false
local IceShield = script.IceShield

UserInputService.InputBegan:Connect(function(input, isChatting)
    if input.KeyCode == Enum.KeyCode.X and not isChatting then
        if not char.Humanoid.Jump then
        -- if the key is X, not chatting, and not jumping we do the following 

            local IceClone = IceShield:Clone()
            IceClone.Parent = game.Workspace
            IceClone:MakeJoints() -- make sure it doesn't fall apart!
            IceClone:SetPrimaryPartCFrame(Torso.CFrame + Torso.CFrame.lookVector*5)
        end
    end
end)

Ad
Log in to vote
0
Answered by
INOOBE_YT 387 Moderation Voter
5 years ago

You need to put

local position = Torso.Position + Torso.CFrame.lookVector * 5

inside the keydown event. This is done because when it is outside of the event, the value of that stays the same even after the character has moved position. When it's inside the keydown event, the position is calculated right when they press it.

0
Also use SetPrimaryPart to move it. INOOBE_YT 387 — 5y
0
stop copying me. User#19524 175 — 5y
0
br0 u posted 24 seconds before me >:CCCC INOOBE_YT 387 — 5y
0
i doubt it's a coincidence you decided to tell OP about the position. i did that. User#19524 175 — 5y
View all comments (2 more)
0
"Okay, so I want to move a model in front of my torso, but that's not working though" the position is the problem, what else is there to answer INOOBE_YT 387 — 5y
0
He didn't refer to the clone when he moved it. That's another problem. User#19524 175 — 5y

Answer this question