So, as the title says, I'm making a gun, and you can shoot through walls with certain camera angles. What I want to do is make the Raycast resize itself to not go through the wall. As You can see in the script, I already tried something, but it Didn't work. I don't know if I'm doing something wrong with RayCastParams, If I am, how do i solve it?
local Cooldown = false
local NonCollideDescendants = nil
local function MakeBullet(Start, End, Dist)
local Lazer = Instance.new("Part")
Lazer.Parent = game.Workspace
Lazer.Anchored = true
Lazer.CanCollide = false
Lazer.Size = Vector3.new(.1, .1, Dist)
Lazer.CFrame = CFrame.new(Start, End) * CFrame.new(0, 0, - Dist / 2)
Lazer.Name = "Lazer"
end
script.Parent.Event.OnServerEvent:Connect(function(player, FromP, ToP, Hum, Part)
local PlayerStuff = nil
for i, Descendants in pairs(game.Workspace:GetDescendants()) do
if Descendants:IsA("Part") or Descendants:IsA("MeshPart") then
if Descendants.CanCollide == false then
NonCollideDescendants = Descendants
end
end
end
for i, Descendantss in pairs(player.Character:GetDescendants()) do
if Descendantss:IsA("Part") or Descendantss:IsA("MeshPart") then
if Descendantss.CanCollide == false then
PlayerStuff = Descendantss
end
end
end
if Cooldown == false then
Cooldown = true
local Pos = ToP
local CharacterParts = player.Character:GetDescendants()
--local RayCast = Ray.new(script.Parent.Handle.Position, (ToP - FromP).unit * 100)
local RayCast = (FromP - ToP).Unit * 100
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Blacklist
Params.FilterDescendantsInstances = {
PlayerStuff,
NonCollideDescendants
}
--local Part, Position, Normal = game.Workspace:FindPartOnRay(RayCast , player.Character , false , true, RaycastParams)
local RayCastResult = workspace:Raycast(FromP, RayCast, Params)
local Dist = (ToP - FromP).magnitude
if Dist > 100 then
Dist = 100
end
local Sound = script.Parent.Sound
Sound:Play()
if Hum ~= nil then
Hum:TakeDamage(50)
end
MakeBullet(FromP, ToP, Dist)
wait(.28)
Cooldown = false
end
end)
Btw. Sorry For the messy script lol
Edit: I know what the problem is! But I still need help, It's using "ToP" as the part's end Position, and "ToP" is the Mouse Position, so what I need to do Is get the Raycast's end position.. But I don't know how :/

19 comments
sorry too complicated
aw man!
Thanks for trying, anyways
are there any errors, what does it say?
and can you send the script that fires the evnt too?
There's absolutly no errors, the problem is that the part is created from the start position, to the mouse's position. I want the Part to end at the RayCastResult's end position
Ohh ok I could try to fix it
Ohh ok I could try to fix it
thanks!
I'll try fix it too
Hi thanks for you comment, can you tell me exactly what createbullet() does, and isnt it makebullet()?
Acoording to your script make bullet contains the start position, end position and the distance, so cou could do: MakeBullet(source, destination, direction)
Hi Thanks I am so glad you faound your solution in so long, I will look into your issue though.
Can you please post your renewed script so I can look into line 75!
Oh ok nevermind i get it
There are 2 ways to solve your issue. the 1st is to barricade the sky in your game..
The 2nd way is to add an if statement, I will post this solution of the ediited version of my answer.
Ok I have edited my answer to fix the problem
Hi I edited my code again check it out now