is it possible to add an accessory to a random player? | Roblox Forum
Welcome to the Forums!
Join thousands of other Roblox members with a free account.
Create Forum Account

is it possible to add an accessory to a random player?

  • This website was created by Roblox members to replace the official forums because Roblox shut them down! Join our community if you like to talk about Roblox!
Status
Not open for further replies.

charliemacdmv

Member
Jul 8, 2020
45
7
8
Roblox
Charliemacdmv
Ok, so many people have tried to help but I still can't get it to work. I appreciate all the help.
I have a murderer set up. Starts with a weapon. How can I have him start with an accessory? I don't want to replace the whole character. Just add a mask.
Below is the whole code for the PlayerManager. This is the code that spawns the Murderer with a weapon. I have tried numerous ways and just can't figure it out. I'm ready to give up on it.

1595686398896.png

Code:
local PlayerManager = {}
-- Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Modules
local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
-- Events
local events = ServerStorage:WaitForChild("Events")
local matchEnd = events:WaitForChild("MatchEnd")
-- Variables
local lobbySpawn = workspace.Lobby.StartSpawn
local arenaMap = workspace.Arena
local spawnLocations = arenaMap.SpawnLocations
local playerWeapon = ServerStorage.Weapon
-- Values
local displayValues = ReplicatedStorage:WaitForChild("DisplayValues")
local playersLeft = displayValues:WaitForChild("PlayersLeft")
local activePlayers = {}
local function checkPlayerCount()
    if #Players == 1 then
        matchEnd:Fire(gameSettings.endStates.FoundWinner)
        print("Found winner")
    end
end
local function removeActivePlayer(player)
    print("removing player")
    for playerKey, whichPlayer in pairs(activePlayers) do
        if whichPlayer == player then
            if whichPlayer.Name == ServerStorage.Players.Murderer.Value then
                matchEnd:Fire(gameSettings.endStates.Dead)
            else
                table.remove(activePlayers, playerKey)
                playersLeft.Value = #activePlayers
                checkPlayerCount()
            end
        end
    end
end
local function respawnPlayerInLobby(player)
    player.RespawnLocation = lobbySpawn
    player:LoadCharacter()
end
local function preparePlayer(player, whichSpawn)
    player.RespawnLocation = whichSpawn
    player:LoadCharacter()
    local character = player.Character or player.CharacterAdded:Wait()
    -- Give the player a tool
    if player.Name == ServerStorage.Players.Murderer.Value then
        local sword = playerWeapon:Clone()
        sword.Parent = player.Backpack
    elseif player.Name == ServerStorage.Players.Sheriff.Value then
        local gun = ServerStorage.Gun:Clone()
        gun.Parent = player.Backpack
    end
end   
    local humanoid = character:WaitForChild("Humanoid")
    humanoid.Died:Connect(function()
        if player.Backpack:FindFirstChild("Gun") or player.Character:FindFirstChild("Gun") then
            local gunGiver = ServerStorage:WaitForChild("Gun")
            local part = gunGiver:Clone()
            part.Parent = workspace
            part.Handle.CFrame = character.HumanoidRootPart.CFrame
            part.Handle.Anchored = true
            print("HIIII")
        end
        respawnPlayerInLobby(player)
        removeActivePlayer(player)
    end)
end
local function onPlayerJoin(player)
    player.RespawnLocation = lobbySpawn
end
local function removePlayerWeapon(whichPlayer)
    -- Check to see if a player exist in case they disconnected or left.
    if whichPlayer then
        local character = whichPlayer.Character
        -- If the player has it currently on their character
        if character:FindFirstChild("Weapon") then
            character.Weapon:Destroy()
        end
        -- If theplayer has the weapon in their backpack
        if whichPlayer.Backpack:FindFirstChild("Weapon") then
            whichPlayer.Backpack.Weapon:Destroy()
        end
        if character:FindFirstChild("Gun") then
            character.Gun:Destroy()
        end
        -- If theplayer has the weapon in their backpack
        if whichPlayer.Backpack:FindFirstChild("Gun") then
            whichPlayer.Backpack.Gun:Destroy()
        end
    else
        print("No player to remove weapon")
    end
end
function PlayerManager.sendPlayersToMatch()
    local availableSpawnPoints = spawnLocations:GetChildren()
    for playerKey, whichPlayer in pairs(Players:GetPlayers()) do
        table.insert(activePlayers,whichPlayer)
        -- Gets a spawn location and then removes it from the table so the next player gets the next spawn
        local spawnLocation = availableSpawnPoints[1]
        table.remove(availableSpawnPoints, 1)
        preparePlayer(whichPlayer, spawnLocation)
    end
    playersLeft.Value = #activePlayers
end
function PlayerManager.getWinnerName()
    if activePlayers[1] then
        local winningPlayer = activePlayers[1]
        return winningPlayer.Name
    else
        return "Error: No player found"
    end
end
function PlayerManager.removeAllWeapons()
    for playerKey, whichPlayer in pairs(activePlayers) do
        removePlayerWeapon(whichPlayer)
    end
end
function PlayerManager.resetPlayers()
    for playerKey, whichPlayer in pairs(activePlayers) do
        respawnPlayerInLobby(whichPlayer)
    end
    activePlayers = {}
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
return PlayerManager
 

CallMeKY

Moderator
Ultra
Feb 25, 2018
1,174
295
83
Maine, USA
Roblox
CallMeKY
Discord
CallMeKY#5012
Ok, so many people have tried to help but I still can't get it to work. I appreciate all the help.
I have a murderer set up. Starts with a weapon. How can I have him start with an accessory? I don't want to replace the whole character. Just add a mask.
Below is the whole code for the PlayerManager. This is the code that spawns the Murderer with a weapon. I have tried numerous ways and just can't figure it out. I'm ready to give up on it.

View attachment 22585

Code:
local PlayerManager = {}
-- Services
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Modules
local moduleScripts = ServerStorage:WaitForChild("ModuleScripts")
local gameSettings = require(moduleScripts:WaitForChild("GameSettings"))
-- Events
local events = ServerStorage:WaitForChild("Events")
local matchEnd = events:WaitForChild("MatchEnd")
-- Variables
local lobbySpawn = workspace.Lobby.StartSpawn
local arenaMap = workspace.Arena
local spawnLocations = arenaMap.SpawnLocations
local playerWeapon = ServerStorage.Weapon
-- Values
local displayValues = ReplicatedStorage:WaitForChild("DisplayValues")
local playersLeft = displayValues:WaitForChild("PlayersLeft")
local activePlayers = {}
local function checkPlayerCount()
    if #Players == 1 then
        matchEnd:Fire(gameSettings.endStates.FoundWinner)
        print("Found winner")
    end
end
local function removeActivePlayer(player)
    print("removing player")
    for playerKey, whichPlayer in pairs(activePlayers) do
        if whichPlayer == player then
            if whichPlayer.Name == ServerStorage.Players.Murderer.Value then
                matchEnd:Fire(gameSettings.endStates.Dead)
            else
                table.remove(activePlayers, playerKey)
                playersLeft.Value = #activePlayers
                checkPlayerCount()
            end
        end
    end
end
local function respawnPlayerInLobby(player)
    player.RespawnLocation = lobbySpawn
    player:LoadCharacter()
end
local function preparePlayer(player, whichSpawn)
    player.RespawnLocation = whichSpawn
    player:LoadCharacter()
    local character = player.Character or player.CharacterAdded:Wait()
    -- Give the player a tool
    if player.Name == ServerStorage.Players.Murderer.Value then
        local sword = playerWeapon:Clone()
        sword.Parent = player.Backpack
    elseif player.Name == ServerStorage.Players.Sheriff.Value then
        local gun = ServerStorage.Gun:Clone()
        gun.Parent = player.Backpack
    end
end
    local humanoid = character:WaitForChild("Humanoid")
    humanoid.Died:Connect(function()
        if player.Backpack:FindFirstChild("Gun") or player.Character:FindFirstChild("Gun") then
            local gunGiver = ServerStorage:WaitForChild("Gun")
            local part = gunGiver:Clone()
            part.Parent = workspace
            part.Handle.CFrame = character.HumanoidRootPart.CFrame
            part.Handle.Anchored = true
            print("HIIII")
        end
        respawnPlayerInLobby(player)
        removeActivePlayer(player)
    end)
end
local function onPlayerJoin(player)
    player.RespawnLocation = lobbySpawn
end
local function removePlayerWeapon(whichPlayer)
    -- Check to see if a player exist in case they disconnected or left.
    if whichPlayer then
        local character = whichPlayer.Character
        -- If the player has it currently on their character
        if character:FindFirstChild("Weapon") then
            character.Weapon:Destroy()
        end
        -- If theplayer has the weapon in their backpack
        if whichPlayer.Backpack:FindFirstChild("Weapon") then
            whichPlayer.Backpack.Weapon:Destroy()
        end
        if character:FindFirstChild("Gun") then
            character.Gun:Destroy()
        end
        -- If theplayer has the weapon in their backpack
        if whichPlayer.Backpack:FindFirstChild("Gun") then
            whichPlayer.Backpack.Gun:Destroy()
        end
    else
        print("No player to remove weapon")
    end
end
function PlayerManager.sendPlayersToMatch()
    local availableSpawnPoints = spawnLocations:GetChildren()
    for playerKey, whichPlayer in pairs(Players:GetPlayers()) do
        table.insert(activePlayers,whichPlayer)
        -- Gets a spawn location and then removes it from the table so the next player gets the next spawn
        local spawnLocation = availableSpawnPoints[1]
        table.remove(availableSpawnPoints, 1)
        preparePlayer(whichPlayer, spawnLocation)
    end
    playersLeft.Value = #activePlayers
end
function PlayerManager.getWinnerName()
    if activePlayers[1] then
        local winningPlayer = activePlayers[1]
        return winningPlayer.Name
    else
        return "Error: No player found"
    end
end
function PlayerManager.removeAllWeapons()
    for playerKey, whichPlayer in pairs(activePlayers) do
        removePlayerWeapon(whichPlayer)
    end
end
function PlayerManager.resetPlayers()
    for playerKey, whichPlayer in pairs(activePlayers) do
        respawnPlayerInLobby(whichPlayer)
    end
    activePlayers = {}
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
return PlayerManager

You should just be able to use that AddAccessory function in your preparePlayer function. What's the mask you're trying to use?
Code:
local mask = game:GetService('ServerStorage'):WaitForChild('Mask')

local function preparePlayer(player, whichSpawn)
    player.RespawnLocation = whichSpawn
    player:LoadCharacter()
    local character = player.Character or player.CharacterAdded:Wait()
    -- Give the player a tool & the murderer an accessory
    if player.Name == ServerStorage.Players.Murderer.Value then
        character:FindFirstChildOfClass('Humanoid'):AddAccessory(mask:Clone())
        local sword = playerWeapon:Clone()
        sword.Parent = player.Backpack
    elseif player.Name == ServerStorage.Players.Sheriff.Value then
        local gun = ServerStorage.Gun:Clone()
        gun.Parent = player.Backpack
    end
end

Also, your Died event for dropping a gun on death looks busted. You had an extra end in there, and when I removed it, humanoid and player and everything were unknown variables. Did you mean for it to be more like this?
Code:
local function preparePlayer(player, whichSpawn)
    player.RespawnLocation = whichSpawn
    player:LoadCharacter()
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")
    -- Give the player a tool
    if player.Name == ServerStorage.Players.Murderer.Value then
        humanoid:AddAccessory(mask:Clone())
        local sword = playerWeapon:Clone()
        sword.Parent = player.Backpack
    elseif player.Name == ServerStorage.Players.Sheriff.Value then
        local gun = ServerStorage.Gun:Clone()
        gun.Parent = player.Backpack
    end
    humanoid.Died:Connect(function()
        if player.Backpack:FindFirstChild("Gun") or player.Character:FindFirstChild("Gun") then
            local gunGiver = ServerStorage:WaitForChild("Gun")
            local part = gunGiver:Clone()
            part.Parent = workspace
            part.Handle.CFrame = character.HumanoidRootPart.CFrame
            part.Handle.Anchored = true
        end
        respawnPlayerInLobby(player)
        removeActivePlayer(player)
    end)
end
 
Last edited:

charliemacdmv

Member
Jul 8, 2020
45
7
8
Roblox
Charliemacdmv
Yes, that was an accident. So your method worked. I tried similar things. After reading your's I see where I went wrong. But I was close. I'll just keep learning. Thanks again!!!
 
  • Like
Reactions: CallMeKY
Status
Not open for further replies.
General
Help Users
  • No one is chatting at the moment.
    Thecoolguy16183 Thecoolguy16183: hey @Vipnoob07 i added you on roblox friend me its thecoolguy16183