What is wrong with my raycast script?

I’m scripting a raycast move that shoots beams down from the sky at people. I have no idea why it won’t work, I have tried a bunch of stuff.
I’m not getting any errors either



coroutine.resume(coroutine.create(function()
	for _ = 1, 300 do
		wait(1)
		local Players = game:GetService("Players")

			for n, Player in pairs(Players:GetPlayers()) do
				if Player.Character ~= nil and Player.Character:FindFirstChild("Humanoid") then
				
				
				local params = RaycastParams.new()
				--params.FilterDescendantsInstances = {script.Parent}
				--params.FilterType = Enum.RaycastFilterType.Blacklist

				local origin = script.Parent.Position
				local dir = game.Workspace.TestingDummer.Torso.Position

				local result = workspace:Raycast(origin, dir, params)

				if result then
print("yeah")
					local between = (result.Position - origin)
					local midpoint = origin + (between.Unit * between.Magnitude) / 2

					local part = Instance.new("Part")
					part.Anchored = true
					part.CanCollide = false
					part.BrickColor = BrickColor.Red()
					part.Material = Enum.Material.Neon
					part.Parent = workspace

					part.CFrame = CFrame.new(midpoint, origin)
					part.Size = Vector3.new(4, 4, between.Magnitude)
					part.Transparency = 0
				end
				
				
				
				
				
				
				

		end

				
				
				
				
				
				
			end
		end
end))

I’d appreciate it if you could help. I’ve been working on this for a good 5 hours now, Different iterations of the move. I thought raycast would be the easiest to do. Apparently not.

The 2nd parameter of Raycast is a direction, not a position. If you want to convert the position to a direction then you would need to subtract “origin” from the end position (workspace.TestingDummer.Torso.Position)

Yes that is it.

Second parameter should be a direction

To get a direction simply do Position1 - Position2 for a vector from position2 to position1
I made a script before that you can see where your raycasts are going

local raycastView = Instance.new("Part")
raycastView.Anchored = true
raycastView.CanCollide = false
local function visRay(startPosition,direction,colour)
    local endPosition = startPosition + direction
    local midpoint = (startPosition + endPosition) /2
    local ray = raycastView:Clone()
    ray.Parent = workspace
    ray.Size = Vector3.new(0.2,0.2s,direction.Magnitude)
    ray.CFrame = CFrame.lookAt(midpoint,start)
    DebrisService:AddItem(ray,updateSpeed)
    --Ignore this if you do not care about the colour :)
    if colour == "red" then
    	ray.BrickColor =BrickColor.new("Really red")
    elseif colour == "green" then
    	ray.BrickColor =BrickColor.new("Bright green")
    elseif colour == "blue" then
    	ray.BrickColor = BrickColor.new("Bright blue")
    elseif colour == "white" then
		ray.BrickColor = BrickColor.new("Institutional white")
    end
    ray.Material = "Neon"
end

Maybe i misunderstood, but it doesen’t seem to be working

coroutine.resume(coroutine.create(function()
	for _ = 1, 300 do
		wait(1)
		local Players = game:GetService("Players")

		for n, Player in pairs(Players:GetPlayers()) do
			if Player.Character ~= nil and Player.Character:FindFirstChild("Humanoid") then


				local params = RaycastParams.new()
				--params.FilterDescendantsInstances = {script.Parent}
				--params.FilterType = Enum.RaycastFilterType.Blacklist

				local origin = script.Parent.Position
				local dir = script.Parent.Position - game.Workspace.TestingDummer.Torso.Position

				local result = workspace:Raycast(origin, dir, params)

				if result then
					print("yeah")
					local between = (result.Position - origin)
					local midpoint = origin + (between.Unit * between.Magnitude) / 2

					local part = Instance.new("Part")
					part.Anchored = true
					part.CanCollide = false
					part.BrickColor = BrickColor.Red()
					part.Material = Enum.Material.Neon
					part.Parent = workspace

					part.CFrame = CFrame.new(midpoint, origin)
					part.Size = Vector3.new(4, 4, between.Magnitude)
					part.Transparency = 0
				end
			end
		end
	end
end))

Try

game.Workspace.TestingDummer.Torso.Position - origin

Works now, thanks a ton!

I have another issue if you want to help out, you don’t gotta but


	local function GetRandomPos (position, Xsize, Zsize, Yheight)
		local randomX = math.random(-Xsize/2, Xsize/2)
		local randomZ = math.random(-Zsize/2 , Zsize/2)
		local spawnAt = Vector3.new(randomX, Yheight, randomZ)
		-- spawnAt is relative to baseplate position
		-- Divide Xsize into left (negative) and right (positive)
		-- same with Zsize because position is at center of part

		return spawnAt + position
	end
		
		
	local	function peepoo()
		local randomPos = GetRandomPos(workspace.THEBASEPLATE.Position, workspace.THEBASEPLATE.Size.X, workspace.THEBASEPLATE.Size.Y, 200)
		
		
		
		
		coroutine.resume(coroutine.create(function()
			for _ = 1, 50 do
			wait(.02)
			local clone = game.ReplicatedStorage.Slime:Clone()
			clone.Script.Disabled = false
			local randomPos = GetRandomPos(workspace.THEBASEPLATE.Position, workspace.THEBASEPLATE.Size.X, workspace.THEBASEPLATE.Size.Y, 200)
			clone.Parent = workspace
			clone.Position  = randomPos
			print(clone.Position)
			game.Debris:AddItem(clone, 2)

			end
		end)) 

This script spawns the slime that shoots the beams of light at the dummy, For some reason it doesen’t seem to be spawning across the map like it should be

It uses the baseplate as the spawn radius

(i fire the function lower down in the script)

Where is it spawning? ​​​​​​​​​​​​​​​

No idea, i don’t think it spawns at all, lemme check the workspace

Doesen’t spawn at all. No idea why

Is Slime just a brick or a model? Any errors in output?

its a brick, No errors

confused

:frowning: