Bullets not positioned correctly

What i want to do is clone a projectile from a prefabs folder in ReplicatedStorage, then put it in the workspace positioned at the weapon’s barrel (called ray in the hierarchy) and then propell it forward.
I have a localscript inside the player that detects player input, manages the gun, etc. and i have a server script that recieves values from the localscript (bullet prefab, barrel CFrame, damage and bullet speed)

It seems to work fine… accept for some reason the bullet spawns like 10 studs below the player.

I have tried printing both the position of the barrel and the position of the CFrame value passed to the server side script. they both print out the same value (sometimes they are slightly different, when the player moves for example, where it prints out the ray value, and the server recieves the value from the client script a bit later)

here are my two scripts:

–Server script:

local Debris = game:GetService(“Debris”)

game.ReplicatedStorage.ServerRemotes:WaitForChild(“Fire”).OnServerEvent:Connect(function(plr,
bullet, pos, baseDmg, speed)
local bolt = bullet:Clone()
bolt.Parent = game.Workspace

if bolt:FindFirstChild("BodyVelocity") then
	local force = bolt.BodyVelocity
	
	bolt.CFrame = pos
	bolt.Position = pos.Position
	force.Velocity = bolt.CFrame.LookVector * speed
	
	Debris:AddItem(bolt, 5)
else
	print("No BodyVelocity found, please add a BodyVelocity to the prefab.")
	bolt:Destroy()
end

end)

.
.
.
.

–localScript

script.Activate.Event:Connect(function(gun)
local Canfire = true
local Projectile = game.ReplicatedStorage.Projectiles.PlasmaSmallBolt

local function onInputBegan(input, process)
	if (process) then return; end
	if (input.UserInputType == Enum.UserInputType.MouseButton1) then
		if Canfire then
			script.Timer:Fire()
			game.ReplicatedStorage.ServerRemotes.Fire:FireServer(Projectile, gun.Ray.CFrame, 45, 100)
		end
	end
end



script.Timer.Event:Connect(function()
	Canfire = false
	wait(.2)
	Canfire = true
end)


game:GetService("UserInputService").InputBegan:Connect(onInputBegan);

end)

–Video (may be a little hard to see)
Problem

2 Likes

When wanting to set a position for a part, don’t use Position, especially when you’re already using the part’s CFrame to set it. Always use CFrame. I’m 99% sure that won’t solve the problem itself but it’s better not to use Position.

2 Likes

Could you possibly change the colour of the bullet and make it non transparent, it is very difficult to see it

2 Likes

the bullet isnt transparent in any way, its just small

1 Like

i used position because CFrame was causing the problem. trying position was an attempt to fix it

1 Like

i removed the position again and it still has not fixed its position

I might be misinterpreting your code here but what it looks like to me is that your bolt cframe = the bullets prefab position, so you’re setting the cframe to the one existing in the properties of that prefab instead of the players barrel

1 Like

wuuuuut

lemme take a look at that xd

its setting bolt’s CFrame to pos. pos is the gun’s barrel position

When working with duplicated objects, unless you’re working with something that can only be set when the part is attached to the DataModel, never set the parent first. Your bullet is being registered into the physics pipeline after you parent it and only afterward you’re setting its properties.

Your bullet is also following automatic network ownership standards which you should try to avoid when it comes to projectile bullets. I don’t know what environment you want it held on but I’ll assume that the server should be in control here.

-- Only change to be made is to the server script at the top:

local Debris = game:GetService("Debris")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.ServerRemotes.Fire.OnServerEvent:Connect(function (plr, bullet, pos, baseDmg, speed)
    local bolt = bullet:Clone()

    if bolt:FindFirstChild("BodyVelocity") then
        local force = bolt.BodyVelocity

        bolt.CFrame = pos
        bolt.Position = pos.Position
        force.Velocity = bolt.CFrame.LookVector * speed

        bolt.Parent = workspace

        bolt:SetNetworkOwner(nil)

        -- Added
        Debris:AddItem(bolt, 5)
    else
        warn("No BodyVelocity for bullet; add to prefab")
        bolt:Destroy()
    end
end)

You should fix your code formatting, since it hurts the readability of this thread. You are able to properly format your code by posting your code between two sets of backticks or using the code block option on your post tools topbar.

1 Like

oh wow, thank you, you have taught me several new things

um… about the debris service?

Oh just add it to the bottom after the SetNetworkOwner call (or if you prefer not using SetNetworkOwner, after the object is parented). Same location as before.

I wasn’t really aiming to provide an extensive code sample for you to copy and paste so any holes in the code can be filled out yourself. Regardless, edited my sample to add that change.

Also a general notice - if you have any extra commentary or information to add, please remember to edit your posts. Add replies when you have more info or commentary that would be reasonable to throw in as a separate post rather than edited in to the same one.

1 Like

ok, thank you

One more thing though… its still spawning at the same place - several studs below the player
i could post the place for you to see what is happening in detail
https://www.roblox.com/games/4400853885/WeaponTest

I’m sure everything’s fine, it’s just that the position is being incorrectly registered.

When the Activate event is fired, it passes some kind of argument. In the script I see, Activate receives this argument as the variable gun. It then passes gun.Ray.CFrame for the position. What is gun.Ray? Where is it located?

If the bullet is spawning far below the player I have two assumptions for the cause of this:

  1. An incorrect position is being sent to the server.
  2. The server is placing bullets wrongly when it uses CFrame and Position (I don’t think using the both of those is necessary?).
1 Like

gun is a variable that contains the local representation of the player’s gun. Ray is a part facing forward and positioned at the front of the gun (at the barrel) The gun model itself is parented to a model called viewModel, which is parented to the camera. This hierarchy is created locally when the player’s character loads in.

Place: https://www.roblox.com/games/4400853885/WeaponTest

hierarchy in studio:
Hierarchy1

runtime hierarchy:
Heirachy2

each time i fire the gun,i noticed the bullet instantiates from a lower position (which would suggest the prefab is falling). But then i checked the prefab in the hierarchy, and sure enough, it wasnt falling (its under ReplicatedStorage, so that wouldnt make sense anyway)

I anchored the weapon, and that seems to have fixed the problem (it gave no notice that it may be falling, which is odd)

thanks for all of your effort guys, this was helpful

1 Like