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!
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)
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
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.
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.
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.
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