How do I fix my chat command?

Hi. I have a script that allows a player with a t-shirt to run a command that allows them to view another player, instead of sorting through every player by another spectate model.

What is the issue? My view command will simply not run.

  1. What solutions have you tried so far? I have looked around the developer forum and asked a few of my friends. Below is my script, located in ServerScriptService.

Screenshot 2024-06-29 at 10.32.56 AM

local MarketplaceService = game:GetService("MarketplaceService")

local gamepassId = 14893150927

local function onPlayerChatted(message)
	local prefix = "/view "
	if string.sub(message:lower(), 1, string.len(prefix)) == prefix then
		local playerName = string.sub(message, string.len(prefix) + 1)
		local targetPlayer = nil
		local playerCount = 0

		for _, player in ipairs(game.Players:GetPlayers()) do
			if string.sub(player.Name:lower(), 1, string.len(playerName)) == playerName then
				targetPlayer = player
				playerCount = playerCount + 1
			end
		end

		if targetPlayer then
			if playerCount > 1 then
				warn("Multiple players found with the name starting with '" .. playerName .. "'. Please provide a more specific name.")
				return
			end

			local player = game.Players.LocalPlayer
			local userId = player.UserId

			local success, isOwned = pcall(function()
				return MarketplaceService:PlayerOwnsAsset(userId, gamepassId)
			end)

			local allowedPlayers = {
				"rvinys",
				"scyfes"
			}

			if success then
				if isOwned or table.find(allowedPlayers, player.Name) then
					local targetCharacter = targetPlayer.Character
					local targetHumanoid = targetCharacter and targetCharacter:FindFirstChild("Humanoid")

					if targetHumanoid then
						
						local origpos = UDim2.new(0,0,0)
						local hiddenpos = UDim2.new(origpos.X.Scale, origpos.X.Offset, origpos.Y.Scale, origpos.Y.Offset + 150)
						player.PlayerGui.SpectatorUI.UI.Position = hiddenpos
						
						player.PlayerGui.SpectatorUI.Enabled = true
						game:GetService("TweenService"):Create(player.PlayerGui.SpectatorUI.UI, TweenInfo.new(0.15, Enum.EasingStyle.Sine), { Position = origpos }):Play()
						
						player.PlayerGui.SpectatorUI.UI.CurrentUser.Text = targetPlayer.Name
						player.PlayerGui.SpectatorUI.UI.headshotImage.Image = "https://www.roblox.com/headshot-thumbnail/image?userId=" .. targetPlayer.UserId .. "&width=420&height=420"
						player.PlayerGui.SpectatorUI.UI.CurrentUser.BackgroundColor3 = targetPlayer.TeamColor.Color

						local camera = workspace.CurrentCamera
						camera.CameraSubject = targetHumanoid
						warn("Viewing " .. targetPlayer.Name)
					else
						warn("Target player does not have a Head")
					end
				else
					warn("Player doesn't own the game pass and is not authorized")
					MarketplaceService:PromptPurchase(player, gamepassId)
				end
			else
				warn("Error occurred while checking game pass ownership")
			end
		else
			warn("Target player not found")
		end
	end
end

game.Players.LocalPlayer.Chatted:Connect(onPlayerChatted)

Any help would be appreciated! I am fairly new to scripting, and this is my first post on the DevForum.

1 Like

I have found a solution to this but change it so that it works with your requirement things (create a remote event named ViewPlayer in ReplicatedStorage)

Sorry if it doesn’t work the way you intended it I’m not that much of a good scripter

Server Script (place it in ServerScriptService):

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local viewPlayer = replicatedStorage:FindFirstChild("ViewPlayer")

local viewCmd = "/view "

local function onPlayerAdded(player)
	local function onPlayerChat(msg)
		if msg:lower():sub(1, #viewCmd) == viewCmd then
			local target = nil
			local targetName = msg:sub(#viewCmd + 1)
			
			for _, otherPlayer in pairs(players:GetPlayers()) do
				if otherPlayer.Name:lower():sub(1, #targetName) == targetName then
					target = otherPlayer
					break
				end
			end
			
			if target and target ~= player then
				viewPlayer:FireClient(player, target)
			end
		end
	end
	
	player.Chatted:Connect(onPlayerChat)
end

players.PlayerAdded:Connect(onPlayerAdded)

Client Script (place in ReplicatedFirst or StarterPlayerScripts):

local replicatedStorage = game:GetService("ReplicatedStorage")
local viewPlayer = replicatedStorage:WaitForChild("ViewPlayer")

local camera = workspace.CurrentCamera

local function viewPlayerEvent(player)
	local humanoid = player.Character:FindFirstChild("Humanoid")
	camera.CameraSubject = humanoid
end

viewPlayer.OnClientEvent:Connect(viewPlayerEvent)

Video of me testing it (also a note, Client scripts cannot run on any Server-related service)

1 Like

Chatted won’t fire if your textservice is not legacy.