Hey guys! How you doing ? I hope very very well! , Being direct, I made a Raycast system, but it doesn’t seems to work at 100% because of an error:
I haven’t seen this error in my experience as a developer, and I don’t really know how to fix this since I’ve done this, this way, and now it gives me this error.
I really appreciate your help and your person if you can give me a hand with this, even if it’s a small one. And with this said, here’s the explication:
-
What do you want to achieve? Making a raycast system that goes from your right hand (rayOrigin) to your mouse position without any errors
-
What is the issue?
-
What solutions have you tried so far? Remaking new raycast scripts but they don’t work as well, so now I use the most well-made one of them.
What you need to know to understand the code is the following things:
- 1 -) There is 3 scripts (1 LocalScripts and 2 ServerScript). The 1st one (ServerScript) creates and parents the “beam” effect to the player’s character. The 2nd (LocalScript) is just to get the mouse position and send the RemoteEvent to the Server. And the 3rd one (ServerScript) is just the handler of the RemoteEvent.
Here’s the LocalScript (the “mousePos” one)
wait(5)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mousePos
local char = player.Character
local rayOrigin = char.RightHand.Position
player.CharacterAdded:Connect(function()
--Give the Beam effects to the player
game.ReplicatedStorage:WaitForChild('RemoteEvents').GiveBeamToCharacter:FireServer(player)
end)
mouse.Move:Connect(function()
mousePos = mouse.Hit.Position
game.ReplicatedStorage:WaitForChild('VoteForSomeoneWithHands'):FireServer(player, rayOrigin, mousePos)
end)
-Here’s the 1st ServerScript (the “Beam effect giver” one) [This seems to work correctly]
game.ReplicatedStorage:WaitForChild('RemoteEvents').GiveBeamToCharacter.OnServerEvent:Connect(function(player)
local BeamPartGuide = Instance.new('Part')
BeamPartGuide.Name = "BeamPartGuide"
BeamPartGuide.CanCollide = false
BeamPartGuide.Anchored = true
BeamPartGuide.Transparency = 0.9
BeamPartGuide.Parent = player.Character
local AlignPosition = Instance.new('AlignPosition')
AlignPosition.Parent = player.Character:FindFirstChild('RightHand')
AlignPosition.Name = "AlignPosition"
local Attachment0 = Instance.new('Attachment')
Attachment0.Parent = player.Character:FindFirstChild('RightHand')
Attachment0.Name = "Attachment0"
local BeamEndAttachment = Instance.new('Attachment')
BeamEndAttachment.Parent = player.Character:WaitForChild('RightHand')
BeamEndAttachment.Name = "BeamEndAttachment"
local Beam = Instance.new('Beam')
Beam.Parent = player.Character:FindFirstChild('RightHand')
Beam.Name = "Beam"
Beam.Attachment0 = Attachment0
Beam.Attachment1 = BeamEndAttachment
end)
-The “mousePos RemoteEvent” handler (ServerScript)[Here’s where it shows the error]
local function MakeVisibleRayWithBeam(rayOrigin, RayDirection, char)
local midPoint = rayOrigin + RayDirection/2
--Block Part(BeamGuide) of the code
local BeamPartGuide = char:FindFirstChild('BeamPartGuide')
BeamPartGuide.Anchored = true
BeamPartGuide.CFrame = CFrame.new(midPoint, rayOrigin)
BeamPartGuide.Size = Vector3.new(0.1, 0.1, RayDirection.Magnitude)
--Beam part of the code
char:FindFirstChild('Beam').Enabled = true
char:FindFirstChild('Beam').Size = BeamPartGuide.Size
char:FindFirstChild('Beam').Position = BeamPartGuide.Position
--char:FindFirstChild('BeamEndAttachment').Position = char.BeamPartGuide.Position
end
game.ReplicatedStorage:WaitForChild('VoteForSomeoneWithHands').OnServerEvent:Connect(function(player, rayOrigin, mousePos)
local char = player.Character
local RayCastParameters = RaycastParams.new()
RayCastParameters.FilterDescendantsInstances = player.Character:GetChildren() --Here this includes the BeamPart and the BeamPartGuide (Because they are inside the player's character)
RayCastParameters.FilterType = Enum.RaycastFilterType.Blacklist
local Range = 100 --Distance of how long can it go
local RayDirection = (mousePos - rayOrigin).Unit * Range --Range --Here's where the "arithmetic error" is produced
-- Cast the ray
local raycastResult = workspace:Raycast(rayOrigin, RayDirection, RayCastParameters)
MakeVisibleRayWithBeam(rayOrigin, RayDirection, char)
if not raycastResult then
raycastResult = {Position = RayDirection}
end
end)