Global 'Raycast' is never read before being written. Consider changing it to local

I just started to make a script but got this error and I’m unsure what it means.

RunService.Heartbeat:Connect(function()
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {Character}
	Params.FilterType = Enum.RaycastFilterType.Exclude

	local Origin = RootPart.CFrame.LookVector * 3

	local Direction = Vector3.new(0, RootPart.CFrame.Position.Y - 5, 0)

	Raycast = workspace:Raycast(Origin, Direction, Params) -- error here
end)

if not (Raycast) then

end
1 Like

Since you haven’t declared a ‘Raycast’ variable yet, you should create it as a local variable like so:

local Raycast = workspace:Raycast(Origin, Direction, Params)

Another thing is, your conditional statement here:

if not (Raycast) then

end

This isn’t going to be checking anything, as the raycast isn’t in the condition’s scope, and even if you stored it outside of the heartbeat, it would only be checking the state of the raycast once, you want to be doing:

RunService.Heartbeat:Connect(function()
    local Params = RaycastParams.new()
    Params.FilterDescendantsInstances = {Character}
    Params.FilterType = Enum.RaycastFilterType.Exclude
    
    local Origin = RootPart.CFrame.LookVector * 3
    
    local Direction = Vector3.new(0, RootPart.CFrame.Position.Y - 5, 0)
    
    local Raycast = workspace:Raycast(Origin, Direction, Params)
    
    if not (Raycast) then
      
    end

    -- Continue code with raycast here, and anything you want to do with it...
end)
1 Like

Whats the error that you are getting? Is the Raycast variable already declared?

I recreated what you wrote with a few assumptions let me know if I am off or this doesn’t work for you:

local RootPart = Character:WaitForChild("HumanoidRootPart") -- Assuming Character is defined and this is the root part you want 

RunService.Heartbeat:Connect(function()
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {Character}
	Params.FilterType = Enum.RaycastFilterType.Exclude

	local Origin = RootPart.Position
	local Direction = Vector3.new(0, -5, 0) -- Assuming you want to cast the ray downwards from where the root part is 

	local Raycast = workspace:Raycast(Origin, Direction, Params)

	if not Raycast then
		-- Handle the case when the ray doesn't hit anything
	else
		-- Handle the case when the ray hits something
	end
end)
1 Like

The code is now this, and the error doesn’t show but I still don’t know what it meant.

RunService.RenderStepped:Connect(function()
	if Humanoid.FloorMaterial ~= Enum.Material.Air then
		local Params = RaycastParams.new()
		Params.FilterDescendantsInstances = {Character}
		Params.FilterType = Enum.RaycastFilterType.Exclude

		local Origin = RootPart.Position + RootPart.CFrame.LookVector * 2

		local Direction = -RootPart.CFrame.UpVector * 10

		local Raycast = workspace:Raycast(Origin, Direction, Params)

		if not (Raycast) then

		end
	end
end)

It looks like the error you had was because you didn’t tell the script that “Raycast” was a variable before instantiating workspace:Raycast(Origin, Direction, Params) to it so it basically was writing to something it couldn’t, hence the “Never read before being written error”

Slapping a local behind it declares it as a read/write variable thus resolving your issue.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.