Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Moving a model in front of my torso?

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)

1 comment

0

Fixed

2 answers

✓ Accepted answer

###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)

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.

6 comments

0

Also use SetPrimaryPart to move it.

0

stop copying me.

0

br0 u posted 24 seconds before me >:CCCC

0

i doubt it's a coincidence you decided to tell OP about the position. i did that.

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

0

He didn't refer to the clone when he moved it. That's another problem.

This discussion is preserved as it was. New answers, comments, and votes are closed.