Mouse raycast script not working

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am making a tower defense game and I want to have my towers be placed where the mouse clicks.

  2. What is the issue? Include screenshots / videos if possible!
    It keeps erroring out no matter what I do.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve looked at different videos and analyzed my code many times, but I cannot figure out what is wrong in the script.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
It is a localscript inside of a tool and the error says “expected eof, got end”, but in the video i’m using as a guide there are no errors.

local Tool = script.Parent
local RESET_SECONDS = 5
local debounce = false
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera
local mousePosition = UserInputService:GetMouseLocation()
local mouseray = camera:ViewportPointToRay(mousePosition.X,mousePosition.Y)
local raycastresult = workspace:Raycast(mouseray.Origin,mouseray.Direction * 1000)
local item = nil

return raycastresult
end

RunService.RenderStepped:connect(function()
	local result = MouseRaycast()
	if result and result.Instance then
		item = result.Instance
	end
end)

Tool.Activated:connect(function()
	local Players = game:GetService("Players")
	local spawnPos = script.Parent.Handle.MeshPart.Position --Current Position (Spawner)
	local pos = spawnPos --Extra Stoff, don't touch

	if not debounce then
		debounce = true

		script.Parent.Handle.Call:Play()
		task.wait(1) -- Spawn Delay
		local noob1 = game.ServerStorage.AntiNoobyTurret["Noob"]:clone() --Cloning
		noob1.Parent = game.Workspace --Parent
		noob1:MoveTo(item.Position) --Spawn Values (X, Y, Z)
		script.Parent.Handle.Disrupt:Play()
		script.Parent.Handle.Waitmate.Transparency = 0
		--You can also use Math.Random() to calculate your spawns to a random set of values for the Vector3.new() Values
		wait(RESET_SECONDS)
		debounce = false
		script.Parent.Handle.Waitmate.Transparency = 1
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

I’m assuming there was supposed to be a function called MouseRaycast in which returned raycastresult, but you forgot to make it.

local Tool = script.Parent
local RESET_SECONDS = 5
local debounce = false
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera

function MouseRaycast()
	local mousePosition = UserInputService:GetMouseLocation()
	local mouseray = camera:ViewportPointToRay(mousePosition.X,mousePosition.Y)
	local raycastresult = workspace:Raycast(mouseray.Origin,mouseray.Direction * 1000)
	
	return raycastresult
end

local item = nil

RunService.RenderStepped:connect(function()
	local result = MouseRaycast()
	if result and result.Instance then
		item = result.Instance
	end
end)

Tool.Activated:connect(function()
	local Players = game:GetService("Players")
	local spawnPos = script.Parent.Handle.MeshPart.Position --Current Position (Spawner)
	local pos = spawnPos --Extra Stoff, don't touch

	if not debounce then
		debounce = true

		script.Parent.Handle.Call:Play()
		task.wait(1) -- Spawn Delay
		local noob1 = game.ServerStorage.AntiNoobyTurret["Noob"]:clone() --Cloning
		noob1.Parent = game.Workspace --Parent
		noob1:MoveTo(item.Position) --Spawn Values (X, Y, Z)
		script.Parent.Handle.Disrupt:Play()
		script.Parent.Handle.Waitmate.Transparency = 0
		--You can also use Math.Random() to calculate your spawns to a random set of values for the Vector3.new() Values
		wait(RESET_SECONDS)
		debounce = false
		script.Parent.Handle.Waitmate.Transparency = 1
	end
end)

Well, after watching the video again and seeing your reply, I finally realized this was the error, but now i’m running into 2 other problems: First, the turrets are client-side and do not attack the enemies and second, when I click on the floor instead of spawning on top of the floor, the turret appears in the exact position of the center of the part causing it to fall into the void. Any solutions to this?

Well for one, you should handle anything that is supposed to affect the server on the server (meaning it affects and shows for everyone). So, there are a few things I would point out about this. Instead of setting an item, I would put a position of where the Raycast hit, then fire that position to the server for the server to place the turret.

local Tool = script.Parent
local RESET_SECONDS = 5
local debounce = false
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera

function MouseRaycast()
	local mousePosition = UserInputService:GetMouseLocation()
	local mouseray = camera:ViewportPointToRay(mousePosition.X,mousePosition.Y)
	local raycastresult = workspace:Raycast(mouseray.Origin,mouseray.Direction * 1000)
	
	return raycastresult
end

local position = nil
local placeevent = --event to place turret

RunService.RenderStepped:connect(function()
	local result = MouseRaycast()
	if result and result.Position then
		position = result.Position
	end
end)

Tool.Activated:connect(function()
	local Players = game:GetService("Players")
	local spawnPos = script.Parent.Handle.MeshPart.Position --Current Position (Spawner)
	local pos = spawnPos --Extra Stoff, don't touch

	if not debounce and position then
		debounce = true

		script.Parent.Handle.Call:Play()
		task.wait(1) -- Spawn Delay
		placeevent:FireServer(position)
		script.Parent.Handle.Disrupt:Play()
		script.Parent.Handle.Waitmate.Transparency = 0
		--You can also use Math.Random() to calculate your spawns to a random set of values for the Vector3.new() Values
		wait(RESET_SECONDS)
		debounce = false
		script.Parent.Handle.Waitmate.Transparency = 1
	end
end)

Then the place event would look something like this:

local placeevent = --place event

placeevent.OnServerEvent:Connect(function(plr, position)
	local noob1 = game.ServerStorage.AntiNoobyTurret["Noob"]:Clone() --Cloning
	noob1.Parent = workspace --Parent
	noob1:MoveTo(position)
end)

Now, I would add a LOT of sanity checks for this (basically making sure placing there should be possible and that the player is able to do this).

Ok, I understand everything except for “local placeevent = --place event”, can you explain more what the place event would be?

the place event would be a remote event.

ok i think i get it now, im guessing that the script itself inside of the remote event would be the code you put but replacing placeevent with script.Parent
update: i did it all, it works pretty much perfectly, the only problem is as soon as i click run it says remotevent is not a valid member of local script and i have to clone it to work, but everything else is perfect, thanks for the help
update 2: to fix it, i just disabled the script and added a script inside that activates it after 1 second and now it is perfect and was easily fixed

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