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

How do i move a projectile to the player mouse position? [NON FE]

Asked by 5 years ago

Greetings everybody, i was just making a pistol with my basic scripting knowledge and it seems to keep setting position as (0,0,0) in Vector3. However, it does not error which is fortunately good. I will show you my current code.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
script.Parent.Equipped:Connect(function()
mouse.Button1Down:Connect(function()
local projectile = Instance.new("Part",workspace)
projectile.Size = projectile.Size - Vector3.new(1.8,1,2)
projectile.Position = Vector3.new(mouse.hit)
print(projectile.Position)
if mouse.Target == "Head" then
    mouse.Target:WaitForChild("Humanoid")
    print("hum")
end
end)
end)

You can decide if you want to make it into FE, but thats your own choice i will never beg someone or say that you have to make it into FE for me.

Anyways, have a merry christmas and a happy new year! ~intepcs

2 answers

Log in to vote
1
Answered by 5 years ago

Code

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

script.Parent.Equipped:Connect(function()
    mouse.Button1Down:Connect(function()
        local projectile = Instance.new("Part") -- Using the second parameter of Instance.new is not recommended. Parent it in a seperate line instead.
        projectile.Size = projectile.Size - Vector3.new(1.8,1,2)
        projectile.Position = mouse.Hit.Position -- Hit returns a .CFrame value, and .Position uses Vector3. Put .Position after Hit to convert it to Vector3.
        -- Also you don't have to put the Vector3.new in as mouse.Hit.Position is a Vector3 itself.
        projectile.Parent = workspace -- I prefer to parent it after all the properties are set.
        print(projectile.Position)
        mouse.TargetFilter = projectile -- Note mouse.Target will always be the projectile. TargetFilter makes .Target ignore the projectile.
        if mouse.Target ~= nil then -- Check if it's not nil so checking its name won't return an error.
            if mouse.Target.Name == "Head" then -- Add .Name to get the instance's name.
                mouse.Target:WaitForChild("Humanoid") -- Note it's trying to find a humanoid inside the head. It will yield forever if there isn't one.
                print("hum")
            end
        end
    end)
end)

Problems

The reason your position keeps returning as (0, 0, 0) is because you put a CFrame into Vector3.new, and I guess that doesn't really work out so it returns those numbers. I fixed it by putting mouse.Hit.Position(Adding .Position to a CFrame value converts it to a Vector3), and removed the Vector3.new(mouse.Hit.Position already is a Vector3 itself).

There's also some other problems I found with your script. mouse.Target would always return the projectile you just created, so I used the TargetFilter property in the mouse to make the mouse ignore the part. Plus, you need to add .Name after mouse.Target to the the name of the target.

Also, note this script only works locally. That means your script does work, but only shows on your client. It does not show for everyone else. If you want to make everyone else see the parts being created, you'll have to use something called remote events, which lets clients and the server communicate with each other.

Hope this helped! If you have any questions, just comment!

Ad
Log in to vote
0
Answered by 5 years ago

@intepcs What your script was missing was the Velocity property. That's what's actually moving the projectile. This should do :


local player = game.Players.LocalPlayer local mouse = player:GetMouse() local gun = script.Parent local handle = gun.Handle gun.Equipped:Connect(function() mouse.Button1Down:Connect(function() local projectile = Instance.new("Part", workspace) projectile.Size = Vector3.new(0.5,0.5,2) projectile.CFrame = handle.CFrame * CFrame.new(0,0,-1) projectile.BrickColor = BrickColor.new('Really red') projectile.Velocity = handle.CFrame.LookVector * 300 print(projectile.Position) if mouse.Target and mouse.Target.Parent : FindFirstChild('Humanoid') then print("hum") end end) end)

Answer this question