View command wont work

  1. What do you want to achieve?
    I’m trying to make a “!view [Target]” where it makes you spectate the target.

  2. What is the issue?
    It isn’t working!

Here is my code for module script:

function Commands.view(player, args)
	local starter = player
	player = nil
	local plrs = game.Players:GetPlayers()
	for _,v in pairs(plrs) do
		if string.match(v.Name:lower(), args[2]) then
			player = v
			break
		end
	end
	if not player then
		warn(args[2] .. ' does not exist.')
	end
	local char = player.Character
	if not char then return end
	local re = player.Character.CameraManipulation.RemoteEvent
	re:FireClient(player, player.Character)
end
  1. What solutions have you tried so far?
    I’ve done anything I could think of. I tried using remote events in any way possible I could think of but nothing worked.

My script that gets the module:

local commands = require(script.Commands)
local prefix = "!"

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		msg = string.lower(msg)
		local split = string.split(msg,' ')
		local cmd = split[1]
		if cmd:sub(1,1) ~= prefix then return end
		if commands[cmd:sub(2,#cmd)] then
			commands[cmd:sub(2, #cmd)](player,split)
		end
	end)
end)

What are your errors in the output if you have some?

No errors were in the output relating to the script.

function Commands.view(**player**, args)
	local starter = player
	player = nil
	local plrs = game.Players:GetPlayers()
	for _,v in pairs(plrs) do
		if string.match(v.Name:lower(), args[2]) then
			**player** = v
			break
		end
	end
	if not player then
		warn(args[2] .. ' does not exist.')
	end
	local char = player.Character
	if not char then return end
	local re = player.Character.CameraManipulation.RemoteEvent
	re:FireClient(player, player.Character)
end

Your issue is that the script is working as expected, but you are firing the remote event wrong. Player is already defined when you call Commands.view, but then you change that value when you iterate through players to find your target and you fire the remote event to that player to view their own character by what I am observing. Change re:FireClient(player, player.Character) to re:FireClient(starter, player.Character) and that may just do the trick :] :wink:

I ran it but the local script didn’t run at all. I don’t understand why.

Not sure if this is useful but here’s the code for the local script:

local re = script:WaitForChild("RemoteEvent")

re.OnClientEvent:Connect(function(target)
	workspace.CurrentCamera.CameraSubject = target
end)