Raycasting Acting Weird?

if you saw my last post on fixing a checkpoint system, I’ve been having trouble with scripting commissions and have been puzzled with these for a bit.

Anyways, I’ve been having trouble with raycasting for the past couple of days.
(some of this is from a raycasting tutorial, I’m still learning.)
My Problem:
My bullets show in a weird spot that isn’t coming from the tool’s shootpart.

Code:


local tool = script.Parent
local config = tool:WaitForChild("Configuration")
local shoot_part = tool:WaitForChild("Shoot")
local remote = tool:WaitForChild("OnShoot")

local Workspace = game:GetService("Workspace")
local ServerStorage = game:GetService("ServerStorage")

remote.OnServerEvent:Connect(function(player, position) -- position = mouse position
	local origin = shoot_part.Position
	local direction = (position - origin).Unit * 300
	local result = Workspace:Raycast(origin, direction)
	
	local intersection = result and result.Position or origin + direction
	local distance = (origin - intersection).Magnitude
	
	local bullet_clone = game.ReplicatedStorage.Bullet:Clone()
	bullet_clone.Size = Vector3.new(0.1, 0.1, distance)
	bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, - distance / 2)
	bullet_clone.Parent = workspace
	game:GetService("Debris"):AddItem(bullet_clone, 3)
	if result then
		local part = result.Instance
		local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
		
		if humanoid then
			humanoid:TakeDamage(config.Damage.Value)
		end
	end
	
	
	
end)


This could be a simple fix but I’m not sure.
Thank you!

1 Like

I don’t know why you would need ray casting since there is a VERY easy fix to this, but only works on a local script:

use Player:GetMouse()

and when you click just use this:

local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()

     local RayPos = Mouse.Hit.Position -- I named it raypos for you but that is where the mouse it pointed at in a Vector3 position

--Code (How the bullets shoot, just weld a little part to the place that you want the bullet to shoot from!)

end)
1 Like

This brings me closer to simplifying my code, but I’m also not sure how I would send this to the server to process the bullet and damage.

the easy thing is if you have a remote event then you can just add a parameter for the Mouse.hit.Position

1 Like

I’m still having troubles with CFrame, (I’m not sure what to replace the distance and things like that with.

don’t use distance just use CFrame:lerp() or Position:Lerp() (tweening also works but lerping is basically tweening but 0 to 1 and every frame of a tween.

if you use lerping you can do this

local Bullet = -- your Bullet 

Bullet.CFrame = gun.ShootPart.CFrame (Set the position and direction to where the bullet will shoot from)

for i = 0,1,0.1 do --0.1 is speed, make sure that you can add the speed enough times to make 1
 
      Bullet.Position:Lerp(HitPos,i)

end
1 Like

I think the problem for me is just that the bullet isn’t being set in the shoot part, it spawns in the angle under the baseplate for some reason. I’ve tested your code and it hasn’t fixed my main problem.

are you using a tool as your gun, if so then put the tool in the workspace and send a screenshot of ur screen

1 Like

you have to make a displacement so that the bullet leaves a specific place
I made a quick implementation
Baseplate.rbxl (23.4 KB)

1 Like

I’m away so I can’t explain most of this but try looking at what I did here:
secure.rbxl (39.2 KB)

It’s kind of a mess but you should be able to understand the basic things from it. It moves bullets on the client using BindToRenderStep (on all clients of course, not just the client who fired)

This also has sanity checks (try deleting a wall on the client and then shooting through it, it won’t work) and some form of lag compensation.

In the example, the blue bullet is what the client sees and the red bullet is what all the other clients will see. In the above thing I told you to do, you’ll find that the blue bullet will go through the wall and do nothing, but the red bullet will stop at where the part once was and won’t deal damage.

1 Like

Screen Shot 2020-10-05 at 6.17.38 PM

Gun is a template, this entire system is for a commission.

I’ll have to check this out, thank you. If I figure it out I’ll mark it as a solution.

Update:

Alright everyone that has responded to me so far, I haven’t gotten to @Zephyr_42 's system, though the bullet still goes under the baseplate, nothing near the shoot part. Thank you to everyone that’s helped so far, I’m sure it’s getting me closer to my goal.

Hoping to get this fixed by tomorrow, I still have grenades to script and auto guns.

Another Update, Still having issues with this, not sure what to do but I’m still looking into it.

Final Update everyone! I found the culprit to the problem, my code wasn’t wrong. Super sorry for everyones time and I should’ve checked my welds, because my shoot part wasn’t welded therefore it was falling out of the map and glitching. Thank you to everyone who helped :heart: