Search code examples
vectorluaroblox

I need to create a part where the target I shot is at


It's easy to understand. I want to make it so everytime my shot does NOT land on a target (ex: a player), a rounded, grey part appears where my shot hit, in the obstruction, the way Counter-Strike does.

My code consists of a LocalScript, which, when the player holds the left mouse button one down, fires a RemoteEvent, which is picked by a Script and creates the hypothetic ball.

I tried substracting the direction-i-shot's Z value and subtract it by that of the object I shot, accidentally (you are supossed to shot at the target, not at the wall, you know), and sending that vector3 value to a script, so it could create the ball. However, the splinters end up appearing in a strange location, usually far away from the obstruction and to its far left or right.

The code in question:

if raycast then
                
                local mf = raycast.Instance.Position
                local hitCharacter = raycast.Instance.Parent
                local diferencia = Vector3.new(direction.X, direction.Y, direction.Z - mf.Z)
                local shot = hitCharacter:FindFirstChild("Humanoid")
                
                print ("donde disparaste:", direction)
                print("donde esta lo que disparaste:", mf)
                print ("diferencia entre lo q disparé y donde disparé:", diferencia)
                
                if shot then
                    if hitCharacter.Name ~= player.Character.Name then
                        baleado:FireServer(tool, shot, damage)
                    end
                elseif not shot then
                    esquirla:FireServer(startPosition, diferencia)
                end
                
            end

The script, which catches the remote event fired in the LocalScript:


local remoteEvent = script.Parent.esquirlaEvent
local tool = script.Parent
local debris = game:GetService("Debris")

remoteEvent.OnServerEvent:Connect(function(player, startPosition, diferencia)

    local function crearescombros(startPosition, diferencia)
        local center = startPosition + diferencia / 2
        local distance = diferencia.Magnitude
    
        local visual = Instance.new("Part")
        visual.Parent = game.Workspace.linea
        visual.Anchored = true
        visual.CanCollide = false
        visual.BrickColor = BrickColor.new("Medium stone grey")
        visual.Material = Enum.Material.Plastic
        visual.Transparency = .4
        visual.Shape = Enum.PartType.Ball
        
        visual.CFrame = CFrame.new(center, startPosition)
        visual.Size = Vector3.new(10, 10, 10)
        
        debris:AddItem(visual, .050)
    end

    
    crearescombros(startPosition, diferencia)
end)

Solution

  • I'm not sure how startPosition is created, as I do not see it in your code snippet. However, you don't have to calculate anything. The RaycastResult, which you call "raycast" in your code holds the property "Position" (raycast.Position), this returns the Vector3 position value where the raycast hit. This position can directly be used to place the ball instance there.