if any of the players’ character is nil then the code will break and error, so here’s a better one:
local part = script.Parent
local players = game:GetService("Players")
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
for _, player in pairs(players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.Health = 0
end
end
end)
local part = script.Parent
local players = game:GetService("Players")
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
for _, player in pairs(players:GetPlayers()) do
local character = player.Character
if not character then continue end
local humanoid = character:WaitForChild("Humanoid")
humanoid.Health = 0
end
end
end)
Why would we need to wait for a Character to be added? If they’re already dead by then, then just ignore them and move on
local part = script.Parent
local players = game:GetService("Players")
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
for _, player in pairs(players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.Health = 0
player.leaderstats.Wins =+ 1
end
end
end)
local part = script.Parent
local players = game:GetService("Players")
part.Touched:Connect(function(hit)
local touchPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
if touchPlayer then
for _, player in pairs(players:GetPlayers()) do
local character = player.Character
if character then
local humanoid = character:WaitForChild("Humanoid")
humanoid.Health = 0
end
end
touchPlayer.leaderstats.Wins =+ 1
end
end)
then don’t add the leaderstats to the "player’ variable. The “player” variable is every player in the game, so basically you are adding a win to every player in the game. Try this instead
local part = script.Parent
local players = game:GetService("Players")
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
for _, player in pairs(players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.Health = 0
end
local player = players:GetPlayerFromCharacter(hit.Parent) -- Getting the player of the character that touched the part
if not player then return end -- If the player doesn't exist (aka left or is an npc) then don't bother adding a win
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then -- Check if the leaderstats exist
local wins = leaderstats:FindFirstChild("Wins")
if wins then -- Check if the wins exist as well
wins.Value += 1 -- Add 1 to the wins
end
end
end
end)
Alright, good luck! I would recommend trying to make your code simple but gets the job done so it doesn’t look needlessly complicated
Also I think you can make the code nicer via Guard Clauses, like so
local part = script.Parent
local players = game:GetService("Players")
part.Touched:Connect(function(hit)
local touchPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
if not touchPlayer then return end
for _, player in pairs(players:GetPlayers()) do
local character = player.Character
if not character then continue end
character.Humanoid.Health = 0
end
touchPlayer.leaderstats.Wins += 1
end)
Also I fixed a few things like the compound assignment += being =+ in the example, which would’ve errored and the removal of local humanoid = character:WaitForChild("Humanoid") since it’s going exist by the time this part is touched most likely, unless something removes the humanoid, which is unlikely, but if may think it’ll cause a problem, you could just make another condition in the if statement
local part = script.Parent
local players = game:GetService("Players")
part.Touched:Connect(function(hit)
local touchPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
if not touchPlayer then return end
for _, player in pairs(players:GetPlayers()) do
local character = player.Character
if not character then continue end
character.Humanoid.Health = 0
end
touchPlayer.leaderstats.Wins += 1
touchPlayer.Humanoid.Health = 0
end)
They’re going to be killed in the loop so that’s unneeded, the loop goes through all players in game and sets their health to 0, even the one touching it
local FolderOfParts = -- set it to the folder of parts you made
local Part = -- set to the part you want to kill all + 1 leaderstat and rand color
local ColorCancollideOff = BrickColor.new(-- set this to the color of part)
local Players = game:GetService("Players")
local StatToChangeName = "Points?" -- change to name of the value, MUST BE CASE SENSITIVE.
function RandTable(myTable)
-- iterate over whole table to get all keys
local keyset = {}
for k in pairs(myTable) do
table.insert(keyset, k)
end
-- now you can reliably return a random key
random_elem = myTable[keyset[math.random(#keyset)]]
return random_elem
end
Part.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") then
local Player = Players:GetPlayerFromCharacter(hit.Parent)
for _, v in pairs(Players:GetPlayers()) do
if not v == Player then
v.Character:BreakJoints()
end
end
Player.leaderstats[StatToChangeName].Value += 1
for _, v in pairs(FolderOfParts:GetChildren()) do
if v.BrickColor == ColorCancollideOff then
v.CanCollide = false
end
end
local Rand = RandTable(FolderOfParts:GetChildren())
FolderOfParts:GetChildren()[Rand] = BrickColor.Random()
end
end)
I can’t understand what you’re even trying to do here? Are you trying to do all the 3 things he wants in 1 script? Also, a few things
You’re making a whole lot of variables for things you’re checking once, like the brickcolor to set the part’s cancollide.
Why do you have a function to get a random value from a table when it’s simple enough to write it without a function
Why in the code for killing the players did you exclude the player who touched when even @OP wanted the player to die? Also, it’s going to error if the part that touched it isn’t a player, you made no checks for that
For checking the brickcolor of the part. It’s better in this case to just store the random part in a variable, change its brickcolor and check if it’s the brickcolor to set it to be uncollideable rather than making a loop to check. But if there’s other areas that will change its brickcolor, it’s better to use GetPropertyChangedSignal for the BrickColor. Making a loop to check it’s kinda slow, and also makes no sense why you palced it before setting the brickcolor of the random part
local part = script.Parent
local players = game:GetService("Players")
part.Touched:Connect(function(hit)
local touchPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
if not touchPlayer then return end
for _, player in pairs(players:GetPlayers()) do
local character = player.Character
if not character then continue end
character.Humanoid.Health = 0
end
touchPlayer.leaderstats.Win += 1
end)
It appears it does not add to leaderstats.
Heres the leaderstat script.
local WinDataStore = game:GetService("DataStoreService"):GetDataStore("Win")
game.Players.PlayerAdded:Connect(function(player)
local Folder = Instance.new("Folder",player)
Folder.Name = "leaderstats"
local Win = Instance.new("IntValue",Folder)
Win.Name = "Win"
local data
local success, errormessage = pcall(function()
data = WinDataStore:GetAsync(player.UserId.."-Win")
end)
if success and data ~= nil then
player:WaitForChild("leaderstats"):WaitForChild("Win").Value = data
else
player:WaitForChild("leaderstats"):WaitForChild("Win").Value = 0
end
end)
local function Save(player)
local success, errormessage = pcall(function()
WinDataStore:SetAsync(player.UserId.."-Win",player:WaitForChild("leaderstats"):WaitForChild("Win").Value)
end)
if success then
print("Saved")
end
end
game.Players.PlayerRemoving:Connect(function(player)
Save(player)
end)
game:BindToClose(function()
for i, v in pairs(game.Players:GetPlayers()) do
Save(v)
end
wait(1)
end)
local part = script.Parent
local players = game:GetService("Players")
local deb = false
part.Touched:Connect(function(hit)
local touchPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
if not touchPlayer or deb then return end
deb = true
for _, player in pairs(players:GetPlayers()) do
local character = player.Character
if not character then continue end
character.Humanoid.Health = 0
end
touchPlayer.leaderstats.Win.Value += 1
wait(5)
deb = false
end)
This will prevent the part from working 5 seconds after it was touched before it can be used again
Yeah this is possible, sadly im not able to give you scripts but you can make all other humanoids in the game die except the person who touched it, and you can use if statements for the color part.