How can I convert this local script to a script?

I have honestly no clue and I have nothing else to say besides how can I convert this local script to a script?
Code:

local Player = game.Players.LocalPlayer
local Character  = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()
local Handle = script.Parent:WaitForChild("Handle")
local Fire = Handle:WaitForChild("Fire")

local Debounce = false
local Cooldown = 0.5
local Length = 1

local Speed = 150
local Damage = 30

script.Parent.Activated:Connect(function()
	if not Debounce then
		Debounce = true
		
		local mousePos = Mouse.Hit.p
		Fire:Play()
		
		local Bullet = Instance.new("Part")
		Bullet.Parent = workspace
		Bullet.Size = Vector3.new(0.5, 0.5, 1)
		Bullet.CanCollide = false
		Bullet.Material = Enum.Material.Neon
		Bullet.Color = Color3.new(1, 0, 0)
		Bullet.CFrame = CFrame.new(script.Parent.Handle.CFrame.p, mousePos)

		local BodyVelocity = Instance.new("BodyVelocity")
		BodyVelocity.Velocity = Bullet.CFrame.lookVector * 150
		BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		BodyVelocity.Parent = Bullet
		
		Bullet.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and Character ~= hit.Parent and hit ~= Handle then
				hit.Parent.Humanoid:TakeDamage(Damage)
				Bullet:Destroy()
			end
		end)
		wait(Cooldown)
		Debounce = false
	end
end)

Like all forms of input, the player’s Mouse exists only on their client. It cannot be passed to the server for this reason.

The mouse can’t be accessed from the server, so why not pass whichever property that the server needs to the server? For example, if the server needs the mouse’s hit position, simply pass that directly:

event:FireServer(mouse.Hit)

If you need the server to request specific properties of the mouse, you can use RemoteFunctions

Everything else, I’m pretty sure the server can access.

1 Like

So after trying to do these changes I came up with this but I got an error do you know how I can fix this?
Error:
Players.Rapideed.Backpack.LaserGun.LocalScript.Script:19: invalid argument #2 to ‘new’ (Vector3 expected, got Instance)
New Code:

--Local Script
local Player = game.Players.LocalPlayer
local Character  = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()

local Debounce = false
local Cooldown = 0.5

script.Parent.Activated:Connect(function()
	if not Debounce then
		Debounce = true
		script.RemoteFunction:InvokeServer(Player,Mouse.Hit.p)
		wait(Cooldown)
		Debounce = false
	end
end)

--Script
local Handle = script.Parent.Parent:WaitForChild("Handle")
local Fire = Handle:WaitForChild("Fire")

local Debounce = false
local Cooldown = 0.5

local Speed = 150
local Damage = 30

script.Parent.RemoteFunction.OnServerInvoke = function(Player,mousePos)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	Fire:Play()
	local Bullet = Instance.new("Part")
	Bullet.Parent = workspace
	Bullet.Size = Vector3.new(0.5, 0.5, 1)
	Bullet.CanCollide = false
	Bullet.Material = Enum.Material.Neon
	Bullet.Color = Color3.new(1, 0, 0)
	Bullet.CFrame = CFrame.new(Handle.CFrame.p, mousePos)

	local BodyVelocity = Instance.new("BodyVelocity")
	BodyVelocity.Velocity = Bullet.CFrame.lookVector * 150
	BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	BodyVelocity.Parent = Bullet

	Bullet.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and Character ~= hit.Parent and hit ~= Handle then
			hit.Parent.Humanoid:TakeDamage(Damage)
			Bullet:Destroy()
		end
	end)
	wait(Cooldown)
	Debounce = false
end

Can you please tell me the exact error message?

Sorry I copied and pasted the wrong part.
Error:
Players.Rapideed.Backpack.LaserGun.LocalScript.Script:19: invalid argument #2 to 'new' (Vector3 expected, got Instance)

You don’t need to add the Player parameter to here, since it automatically detects who invoked the RemoteFunction.

You can also write the OnServerInvoke function like this instead, but don’t do it unless it’s needed:

script.Parent.RemoteFunction.OnServerInvoke = function(firedby,Player,mousePos)
2 Likes