So I have this Murder Mystery-Like Code, but the Bloxxer (the character with the sword) dies when he destroys the/a Sheriff. The section of code I believe is malfunctioning is the part that ensures the Sheriff doesn't kill the bystanders; but if Sheriff does kill a bystander, the Sheriff dies immediately. However, these same parameters seem to be happening to the Bloxxer also, Why?
I want it to work so if the Sheriff bloxxes someone other (a bystander) than the Mad Bloxxer then the Sheriff dies. I also want the Mad Bloxxer to remain alive even after he destroys a Sheriff in order to destroy the bystanders.
I don't want the Mad Bloxxer to be destroyed when he destroys a Sheriff.
Now here is the suspicious section of code:
function newCharacter(bloxxedPlayer, character) local humanoid = character:FindFirstChild("Humanoid") if humanoid then humanoid.Died:connect(function() if bloxxedPlayer == sheriff then humanoid:UnequipTools() local backpack = bloxxedPlayer:FindFirstChild("Backpack") if backpack then backpack:ClearAllChildren() end local torso = character:FindFirstChild("HumanoidRootPart") if torso then dropNetGun(torso.Position) end end -- This section determine whether the person bloxxed was the Mad Bloxxer or a bystander, if the latter then the sheriff dies. local tag = humanoid:FindFirstChild("creator") if tag and tag.Value and tag.Value.Parent == game.Players then if tag ~= bloxxer and bloxxedPlayer ~= bloxxer then print(tag.Value) local wrongDoer = tag.Value if wrongDoer.Character and wrongDoer.Character.Parent then local wrongDoerHumanoid = wrongDoer.Character:FindFirstChild("Humanoid") if wrongDoerHumanoid then wrongDoerHumanoid.Health = 0 end end end end end) end end
Lines 59-91 contain the code above in the rest of it below.
local roundTimer = 60 * 5 local intermissionTimer = 20 local serverStorage = game:GetService("ServerStorage") local replicatedStorage = game:GetService("ReplicatedStorage") local debris = game:GetService("Debris") local event = replicatedStorage:WaitForChild("RemoteEvent") local maps = serverStorage:WaitForChild("Maps") local mapHolder = game.Workspace:WaitForChild("MapHolder") local statusTag = replicatedStorage:WaitForChild("StatusTag") local timerTag = replicatedStorage:WaitForChild("TimerTag") --set up score leaderboard --keep track of points --drop sheriff net gun if bloxxed --make sure sheriff doesn't bloxxed bystanders function dropNetGun(pos) local pickup = serverStorage:WaitForChild("NetGun"):WaitForChild("Handle"):clone() pickup.Name = "Pickup" pickup.Anchored = true pickup.CanCollide = false pickup.CFrame = CFrame.new(pos) local sparkles = Instance.new("Sparkles") sparkles.SparkleColor = Color3.new(0, 0.5, 1) sparkles.Parent = pickup local given = false pickup.Touched:connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid and humanoid.Health > 0 then local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player and player ~= bloxxer then local backpack = player:FindFirstChild("Backpack") if backpack and not given then given = true pickup:remove() local netGun = serverStorage:WaitForChild("NetGun"):clone() netGun.Parent = backpack sheriff = player end end end end) for _, player in pairs(game.Players:GetChildren()) do if player and player.Character and player.Character.Parent then local humanoid = player.Character:FindFirstChild("Humanoid") if humanoid and humanoid.WalkSpeed < 16 then humanoid.WalkSpeed = 16 end end end debris:AddItem(pickup, roundTimer) pickup.Parent = game.Workspace end function newCharacter(bloxxedPlayer, character) local humanoid = character:FindFirstChild("Humanoid") if humanoid then humanoid.Died:connect(function() if bloxxedPlayer == sheriff then humanoid:UnequipTools() local backpack = bloxxedPlayer:FindFirstChild("Backpack") if backpack then backpack:ClearAllChildren() end local torso = character:FindFirstChild("HumanoidRootPart") if torso then dropNetGun(torso.Position) end end -- This section determine whether the person bloxxed was the Mad Bloxxer or a bystander, if the latter then the sheriff dies. local tag = humanoid:FindFirstChild("creator") if tag and tag.Value and tag.Value.Parent == game.Players then if tag ~= bloxxer and bloxxedPlayer ~= bloxxer then print(tag.Value) local wrongDoer = tag.Value if wrongDoer.Character and wrongDoer.Character.Parent then local wrongDoerHumanoid = wrongDoer.Character:FindFirstChild("Humanoid") if wrongDoerHumanoid then wrongDoerHumanoid.Health = 0 end end end end end) end end function playerAdded(player) local leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" local points = Instance.new("IntValue") points.Name = "Points" points.Parent = leaderstats leaderstats.Parent = player player.CharacterAdded:connect(function() newCharacter(player, player.Character) end) if player.Character and player.Character.Parent then newCharacter(player, player.Character) end end game.Players.PlayerAdded:connect(playerAdded) for _, player in pairs(game.Players:GetPlayers()) do playerAdded(player) end -- game loop while true do statusTag.Value = "Loading Map" timerTag.Value = -1 --load for random map mapHolder:ClearAllChildren() wait(2) local serverMaps = maps:GetChildren() local randMap = serverMaps[math.random(1, #serverMaps)]:clone() randMap.Parent = game.Workspace.MapHolder wait(2) --wait for contestants while true do wait(5) contestants = {} for _, player in pairs(game.Players:GetPlayers()) do if player and player.Character then local humanoid = player.Character:WaitForChild("Humanoid") if humanoid and humanoid.health > 0 then table.insert(contestants, player) end end end if #contestants >= 3 then break else statusTag.Value = "Waiting For Players" timerTag.Value = -1 end end statusTag.Value = "Match" timerTag.Value = roundTimer --choose bloxxer bloxxer = contestants[math.random(1, #contestants)] --choose sheriff while true do randPlayer = contestants[math.random(1, #contestants)] if randPlayer ~= bloxxer then sheriff = randPlayer break end end --teleport contestants local spawnsModel = randMap:WaitForChild("Spawns") local spawns = spawnsModel:GetChildren() for _, player in pairs(contestants) do if player and player.Character and #spawns > 0 then local humanoid = player.Character:WaitForChild("Humanoid") local torso = player.Character:WaitForChild("HumanoidRootPart") local spawnIndex = math.random(1, #spawns) local spawn1 = spawns[spawnIndex] if spawn1 and torso and humanoid then humanoid.Health = 100 humanoid.WalkSpeed = 16 table.remove(spawns, spawnIndex) torso.CFrame = CFrame.new(spawn1.Position + Vector3.new(0, 3, 0)) local matchTag = Instance.new("StringValue") matchTag.Name = "MatchTag" matchTag.Parent = player.Character local backpack = player:WaitForChild("Backpack") if backpack then if player == bloxxer then local sword = serverStorage:WaitForChild("Sword"):clone() sword.Parent = backpack event:FireClient(player, "Class", "Bloxxer") elseif player == sheriff then local netGun = serverStorage:WaitForChild("NetGun"):clone() netGun.Parent = backpack event:FireClient(player, "Class", "Sheriff") else event:FireClient(player, "Class", "Bystander") end end end end end spawnsModel:Destroy() local localTimer = roundTimer while localTimer > 0 do wait(1) localTimer = localTimer - 1 timerTag.Value = localTimer activeContestants = {} bloxxerActive = false for _, player in pairs(contestants) do if player then local character = player.Character if character then local matchTag = character:FindFirstChild("MatchTag") local humanoid = character:FindFirstChild("Humanoid") if matchTag and humanoid and humanoid.Health > 0 then if player == bloxxer then bloxxerActive = true end table.insert(activeContestants, player) end end end end if #activeContestants <= 1 or not bloxxerActive then break end end --sort game result --display results --bloxxer failed --bloxxer successful --bloxxer captured local gameResults = "PlayersWin" if bloxxerActive then if #activeContestants >= 2 then --bloxxer failed event:FireAllClients("Results", "PlayersWin") else --bloxxer successful gameResults = "BloxxerWins" event:FireAllClients("Results", "BloxxerWins") end else --bloxxer captured event:FireAllClients("Results", "PlayersWin") end --teleport players back to lobby --remove tools local lobbySpawns = {} for _, v in pairs(game.Workspace:WaitForChild("Lobby"):GetChildren()) do if v and v.Name == "SpawnLocation" then table.insert(lobbySpawns, v) end end for _, player in pairs(contestants) do if player then if player.Character then local humanoid = player.Character:FindFirstChild("Humanoid") if humanoid then humanoid:UnequipTools() end local randSpawn = lobbySpawns[math.random(1, #lobbySpawns)] player.Character:MoveTo(randSpawn.Position) local backpack = player:FindFirstChild("Backpack") if backpack then backpack:ClearAllChildren() end end end end end
The problem was line 20.
I was checking for 'tag' instead of 'tag.Value.' I changed 'tag' to 'tag.Value' and it fixed the problem.