How to raycast from an object to the center of the screen

no, thats just untrue. You shouldn’t use it as a crutch because you don’t need to go that advanced until it just stops working completely, and you need to learn how to make your own things for when you need to make that sort of stuff, but you can use it whenever to speed up things, or just get general help. 100% use it whenever you want if you know how to use it and know what you are doing.

not really, just use it only for easy tasks

yeah, as ender is saying, say you have some code and you just want to add basic debug statements too it, just ask chat gpt to that or something similar

I am having issues, I migrated most of the main code for handling the raycast to the local player side but it doesn’t seem to be working at all…

Local script:

local Gun = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Dispenser = script.Parent:WaitForChild("Flare")
local mouse = game.Players.LocalPlayer:GetMouse()

local fireGun = script.Parent:WaitForChild("FireGun")
local OnCooldown = false

Gun.Activated:Connect(function()

	if OnCooldown then return end
	local rayCastParams = RaycastParams.new()
	rayCastParams.FilterDescendantsInstances = {char, script.Parent}
	rayCastParams.FilterType = Enum.RaycastFilterType.Exclude
	local Camera = workspace.CurrentCamera
	local unitRay = Camera:ScreenPointToRay(Camera.ViewportSize.X / 2, Camera.ViewportSize.Y / 2)
	local ray = workspace:Raycast(unitRay.Origin, unitRay.Direction * 500)
	fireGun:FireServer(Dispenser.CFrame, ray)
	OnCooldown = true
	task.wait(0.1)
	OnCooldown = false

end)

Server Script:

local fireGun = script.Parent:WaitForChild("FireGun")

fireGun.OnServerEvent:Connect(function(player, Flare, ray)
	if ray then
		local rayInstance = ray.Instance
		local model = rayInstance:FindFirstAncestorOfClass("Model")
		if model then
			if model:FindFirstChild("Humanoid") then
				if rayInstance.Name == "Head" then
					model:WaitForChild("Humanoid"):TakeDamage(30)
					print(rayInstance)
				else
					print("Hit")
					model:WaitForChild("Humanoid"):TakeDamage(10)
				end
			end
		end
	end
end)
local Gun = script.Parent
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Dispenser = script.Parent:WaitForChild("Flare")
local mouse = game.Players.LocalPlayer:GetMouse()

local fireGun = script.Parent:WaitForChild("FireGun")
local OnCooldown = false

Gun.Activated:Connect(function()
	if OnCooldown then return end

	local Camera = workspace.CurrentCamera
	local unitRay = Camera:ScreenPointToRay(Camera.ViewportSize.X / 2, Camera.ViewportSize.Y / 2)

	fireGun:FireServer(Dispenser.CFrame, unitRay.Origin, unitRay.Direction)
	OnCooldown = true
	task.wait(0.1)
	OnCooldown = false
end)

local fireGun = script.Parent:WaitForChild("FireGun")

fireGun.OnServerEvent:Connect(function(player, flareCFrame, origin, direction)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character, script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude

	local result = workspace:Raycast(origin, direction * 500, raycastParams)

	if result and result.Instance then
		local rayInstance = result.Instance
		local model = rayInstance:FindFirstAncestorOfClass("Model")

		if model and model:FindFirstChild("Humanoid") then
			if rayInstance.Name == "Head" then
				model.Humanoid:TakeDamage(30)
				print(rayInstance)
			else
				print("Hit")
				model.Humanoid:TakeDamage(10)
			end
		end
	end
end)

What you were trying to do here is close, but i just realized, you should actually send the ray from the server with the details of the ray from the local side, try this code it might work

1 Like

Ill try it right now. Thank you!

Hey did it work out well? Any issues?

No issues! Thank you so much! What did I do wrong though for it to not work? I am curious.

Originally you tried calling the camera from the server, which is impossible, the new script gets the information about the ray and then shoots it from the server,

if you are referencing your code after migrating, that was my fault, i shouldve been more clearer on what i was trying to say

1 Like

Its fine! Thank you so much though! I couldn’t focus in class because of that issue.

Sorry to bother you again but the code you provided as a bug, after using a visualization tool to visualize the raycast, I concluded that the raycast was a little under than where the user is pointing, here is an example:

is there a way to fix this issue?

Not sure why this is happening, (im also too lazy to actually look into the code thorouhgly) but you can just offset it or put it into chat gpt as it should be a pretty easy fix

If anybody wants to fix the issue on how it is supposed to work, well all you do is instead of using ScreenPointToRay use ViewportPointToRay, like this: local unitRay = camera:ViewportPointToRay(camera.ViewportSize.X/2, camera.ViewportSize.Y/2, 0) and I also removed the line local mouse = game.Players.LocalPlayer:GetMouse() because it is not used in anywhere in the script.