Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would you visualize a raycast (laser/bullet)?

local replicatedStorage = game:GetService("ReplicatedStorage")
local weaponRemoteEvents = replicatedStorage.remoteEvents.weaponRemoteEvents

weaponRemoteEvents.shootEvent.OnServerEvent:Connect(function(player, mousePos, bulletTravelDistance, toolHandle, damage, toolName)
	local rayParams = RaycastParams.new()
	rayParams.FilterDescendantsInstances = {player.Character}
	rayParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local ray = workspace:Raycast(toolHandle.CFrame.p, (mousePos - toolHandle.CFrame.p) * bulletTravelDistance, rayParams)
	
	if ray then
		local model = ray.Instance:FindFirstAncestorOfClass("Model")
		if model then
			if model:FindFirstChild("Humanoid") then
				model.Humanoid.Health -= damage
			end
		end
	end
end)

Here is the code for the gun. I've tried so many ways to get a working visualizer but I just can't seem to get it to work. Please help. Also here are what the params are:

player = local player
mousePos = player:GetMouse().Hit
bulletTravelDistance = 300
toolHandle = toolHandle part
damage = 10
toolName = ar

1 answer

  1. Get the Startpoint and Hitpoint
  2. Get the Middle of the 2 Points
  3. Create a Part with the length of the distance of the 2 points (Magnitude)
  4. Get the Rotation of the 2 Points

Look up everything that you don't know how to do

the second thing Is maybe :Lerp 0.5 or something

3 comments

0

isn't lerp deprecated? maybe just subtract the startpoint by half of the hitpoint

0

I believe :Lerp() was to be deprecated in the context of animation, with the replacement being tweens.

0

local middlePos = Part1.Position:Lerp(Part2.Position, 0.5) but there is also a more efficient solution

This discussion is preserved as it was. New answers, comments, and votes are closed.