Rays casting; after standing still for a while they wont activate until moving

Alright, I’ve been using rays for a recent game I’m working on.
Though, for some odd reason, after a few hits, it requires more clicks to make the ray hit, and then eventually they will not hit until moving again. Any solutions?

function rays(player) --prints "Hit" when hit, and prints "punched" when activating the script
	if hit ~= true then
	local origin = script.Parent.Parent.Torso
	local ray = Ray.new(origin.CFrame.Position, origin.CFrame.lookVector * 4)
	local p, po = workspace:FindPartOnRayWithIgnoreList(ray, player.Character:GetDescendants())
		
	if p and p.Parent:FindFirstChild("Humanoid") then
		damage(p, po) --damage script sent to Humanoid
		hit = true --cannot repeat, uses Heartbeat to repeat the function
	end
	end
end
1 Like