Issue's with View Command

Hey, trying to make a view command and it isn’t working. Mind helping me?

Code (ModulE Script):

local Players = game:GetService("Players")
local PlayerFromName = require(script.Parent.Parent.PlayerFromName)
local rs = game:GetService("ReplicatedStorage")

local event = rs.Events.viewEvent

return function (admin, splitMessage)
    local viewPlayerName = splitMessage[2]
    local viewPlayer = PlayerFromName(admin, viewPlayerName)
    local camera = workspace.CurrentCamera
    
    if not viewPlayer then
        return
    end
    
    event:FireClient(viewPlayer, camera, viewPlayer)
end

Local Script:

local rs = game:GetService("ReplicatedStorage")
local event = rs.Events.viewEvent

event.OnClientEvent:Connect(function(player, cam, playerName)
    local viewPlayer = tostring(playerName)
    local character = workspace[tostring(player)]
    cam.CameraSubject = character.Humanoid
end)

Error: returns nil

I have a question. Which variables are nil? Assuming the error is in the LocalScript, is player, cam, playerName, or all of them nil?

1 Like

not too sure, but it is probably playerName.

Just to confirm which variable is nil, in the LocalScript, right after the OnClientEvent, can you put this line inside?

print(tostring(player), tostring(cam), tostring(playerName))

(The tostring is just so that we convert nil into a string name, so it doesn’t give an error. (Based off of experience.))

player and playername seems to be nil camera is fine.

image

That’s odd. Sorry for nagging about variables and stuff, but what is viewPlayerName, splitMessage, and the module script PlayerFromName? It also might be helpful if you send the PlayerFromName module script.

viewPlayername is the variable and gets the splitMessage from the command.

Ex: /view me splitmessage would be “me”

Here you go!

local Players = game:GetService("Players")

return function (admin, nameFragment)
	if nameFragment == "me" then
		return admin
	end
	
	local allPlayer = Players:GetPlayers()
	
	for _, player in allPlayer do
		local playerName = player.Name:lower()
		local namesMatch = playerName:sub(1, #nameFragment) == nameFragment
		
		if namesMatch then
			return player
		end
	end
end

It could be that the admin variable is nil. Can you try printing it (in the ModulE script), and tell me what the result is.

yeah its not nil

image

i believe it has something to do with my parameters in my module script

Which one? (You shown be two of them LOL)

both, since the camera is fine

I think the problem is the variable splitMessage. Can you explain exactly what it is? I don’t really know what it is, but all I think I know is that it is a table.

it isn’t that trust me. splitMessage is just the message after the command

Sorry, but just to make sure I’m correct, splitMessage is the chat message the player has sent? Like if the player says “Turkey”, splitMessage would be “Turkey”. Or do you mean the players sees a message on the screen, which would be split message.

player says the command and the split message is toward the commands effect is towards

ex: /kill max123lover2

max123lover2 is the splitMessage

Are you SURE splitMessage isn’t nil? I tried it in a server script, and it just returned nil.

Yeah pretty sure cause other commands work. Uses around the same setup. Will do some more bug testing and such to find a fix. Thank you.

Alright, good luck on your bug-fixing!

This is because you cannot access the workspace.CurrentCamera on the server. You have to call out this property on the client. Oh, and I don’t see you changing the CameraType to Scriptable.