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.
- 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.
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.