Hello! I’m having issues adding a tool to a player’s backpack. I am making a sort of murder mystery copy with snowballs. Would anyone be able to help me with this? I have included screenshots below of the script and explorer. I have also included the raw code.
--Variables--
local replictaedStorage = game:GetService("ReplicatedStorage")
local snowball = replictaedStorage:WaitForChild("Snowball")
local players = {}
local santaPlayer = nil
--Loop--
while true do
game.Players.PlayerAdded:Connect(function(plr)
table.insert(players, plr)
end)
wait(1)
if #players >= 1 and santaPlayer == nil then
local playersInRound = {}
for i, v in pairs(players)do
table.insert(playersInRound, v)
end
local santaNumber = math.random(1, #playersInRound)
santaPlayer = playersInRound[santaNumber]
local santaCharacter = santaPlayer.Character
local clonedSnowball = snowball:Clone()
clonedSnowball.Parent = santaCharacter
end
end
I have tried removing the check to see if a ‘Santa’ has already been chosen. That fixed the issue but then every second it will just give you another snowball.
Thank you for any help that you can provide, I appreciate it.
You’re creating a new snowball every second (per the wait time) and not checking if the character has a snowball equipped or has a snowball in their backpack. For some reason you’re also connecting a PlayerAdded event in a loop – this isn’t necessary.
EDIT - FindFirstChild() returns nil if the instance isn’t found, not false.
--Variables--
local replicatedStorage = game:GetService("ReplicatedStorage") -- you mispelled "replicated" in your original script, not really important
local snowball = replicatedStorage:WaitForChild("Snowball")
local players = {}
local santaPlayer = nil
game.Players.PlayerAdded:Connect(function(plr)
table.insert(players, plr)
end)
game.Players.PlayerRemoving:Connect(function(plr)
if players[plr] ~= nil then
players[plr] = nil
end
end)
--Loop--
while true do
wait(1)
if #players >= 1 and santaPlayer == nil then
local playersInRound = {}
for i, v in pairs(players)do
table.insert(playersInRound, v)
end
local santaNumber = math.random(1, #playersInRound)
santaPlayer = playersInRound[santaNumber]
local santaCharacter = santaPlayer.Character
local player = game.Players:GetPlayerFromCharacter(santaCharacter)
if player.Backpack:FindFirstChild("Snowball") == nil and santaCharacter:FindFirstChild("Snowball") == nil then
local clonedSnowball = snowball:Clone()
clonedSnowball.Parent = santaCharacter
end
end
end
local replicatedStorage = game:GetService("ReplicatedStorage")
local snowball = replicatedStorage:WaitForChild("Snowball")
local began = false
local players = {}
local santaPlayer = nil
local round = {}
game.Players.PlayerAdded:Connect(function(plr)
table.insert(players, plr.Name)
plr.CharacterAdded:Connect(function(character)
character.Humanoid.Died:Connect(function()
if santaPlayer == plr.Name then
santaPlayer = nil
round = {}
for _,p in pairs(game.Players:GetPlayers()) do
p:LoadCharacter()
end
began = false
print("oop")
end
for _,p in pairs(round) do
if p == plr.Name then
p = nil
end
end
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
for _,player in pairs(players) do
if player == plr.Name then
player = nil
end
end
end)
function addTool(Character)
local clonedSnowball = snowball:Clone()
clonedSnowball.Parent = Character
end
function startRound()
if #players >= 1 and santaPlayer == nil then
local playersInRound = {}
for i, v in pairs(players) do
table.insert(playersInRound, v)
table.insert(round, v)
print("Inserted")
end
local santaNumber = math.random(1, #playersInRound)
santaPlayer = playersInRound[santaNumber]
local santaCharacter = game.Workspace:WaitForChild(santaPlayer)
addTool(santaCharacter)
print(santaNumber)
began = true
end
end
while began == false do
wait(1)
startRound()
end
Thank you all for the help! Sorry about the late response, I forgot I had this post open. I also like @CluelessAbd 's solution, but I feel your code is cleaner and more compact.