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

How do I make a player move/ move to part it wont work even though I have all the variables?

Asked by
2mania 14
2 years ago
Edited 2 years ago

Im trying to make a test for my game but i cant even get the test working its about moving my player to a part and i need help because every time i do it, it says its a nil or missing argument when i have all the variables someone please help me script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.KeyDown:Connect(function(key)
    key = key:lower()
    if key == 'e' then
        local part2 = Instance.new('Part')
        part2.Parent = game.Workspace
        part2.CanCollide = false
        part2.Anchored = true
        part2.CFrame = player.Character.Head.CFrame * CFrame.new(0,0,5)
        part2.Name = "Part1"
        player:WaitForChild(Humanoid):MoveTo(Vector3.new(game.Workspace.part2.Name.Position))
    end
end)


0
press view source to see full script cause it cuts off 2mania 14 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

Here are some possible reasons why it's not working:

Mouse.KeyDown is deprecated and superseded by UserInputService which should be used in all new work (the current updates).

• In line 13, you didn't make the word inside the parenthesis of :WaitForChild() a string (word/s inside a quotation mark).

• Also in line 13, you don't need to make a position into a Vector3, it's already a Vector3.

And also in line 13, you tried to get the position from its name, which is a string. Try part2.Position.

And (i promise this is the last one for line 13 i swear) also in line 13, you don't need to get part2 from the workspace, it's already defined in the script. And game.Workspace.part2 will return nil because you named part2 "Part1" and should use game.Workspace.Part1.

Lastly, in line 13, you tried to find the "Humanoid" in player instead of the player's character.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then -- if player pressed "E"
        local part2 = Instance.new("Part", workspace) -- creates a part and automatically puts it to workspace
        part2.CanCollide = false
        part2.Anchored = true
        part2:PivotTo(character:WaitForChild("Head"):GetPivot() * CFrame.new(0, 0, 5))
        part2.Name = "Part1"

        character:WaitForChild("Humanoid"):MoveTo(part2.Position, part2)
    end
end)
Ad

Answer this question