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

Hello Im trying to make a part spawn infront of the player however its not working?

Asked by 3 years ago

Thank you for reading Error code:Vector3 expected, got number) Script:

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

mouse.KeyDown:connect(function(key)
    if key == "q" then
        local torso = workspace:FindFirstChild(player.Name)
        local part = game.ReplicatedStorage.Part:Clone()
        part.Parent = game.Workspace
        part.Position = torso.UpperTorso.Position + 10
        wait(5)
    end
end)

1 answer

Log in to vote
1
Answered by
Dfzoz 489 Moderation Voter
3 years ago
Edited 3 years ago

In line 9 you are trying to add a number to a vector3 value (torso.UpperTorso.Position), which you can't.

A vector3 is a property commonly used for position, rotation, velocity,etc. It consist of three coordinates: X, Y and Z.

To create a vector value you can use Vector3.new(x,y,z), and you can add this value to the vector3 of your player, so you might want to change line 9 for this code:

part.Position = torso.UpperTorso.Position + Vector3.new(0,0,5)

This will create a part 5 studs away from the uppertorso of your character. But if you use this right away you might notice it isn't positioning the block exactly at the front of your character. That's because vector3.new(x,y,z) won't give you the current rotation of your player. So you'll want to use CFrame.lookVector, which basically creates a vector3 poiting 1 stud in the direction a block is facing. You can multiply this vector by any number to increase the distance it will spawn at. Try replacing line 9 again with the following code:

part.Position = torso.UpperTorso.Position +(torso.CFrame.lookVector*5)

You might want to learn more about C and Vector3 in the future, here are some links from roblox API to help you: CFrame Vector3

0
Thank you so much! robot7866 36 — 3y
Ad

Answer this question