Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make an efficient script to kill players in an obby when they touch a part?

I'm trying to make a kill script for when players touch a kill brick in my obby. I tried using the Humanoid.Touched feature, however, this is glitchy. So, I need a better alternative. Could someone please help me? Script:

local CS = game:GetService("CollectionService")
local Players = game:GetService("Players")

function OnHumanoidTouched(otherpart, limb)
	
	local player = Players:GetPlayerFromCharacter(limb.Parent)
	
	if CS:HasTag(otherpart, "Checkpoint") then
		--some checkpoint stuff
				
	elseif CS:HasTag(otherpart, "KillBrick") then
		player.Character:WaitForChild("Humanoid"):TakeDamage(100)
			
	end
end

function OnCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid");
	humanoid.Touched:Connect(OnHumanoidTouched)
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(OnCharacterAdded)
end)

2 comments

0

Not using .Touched because it fires multiple times

0

And its glitchy

2 answers

When the player joins, there is a period of time where the player does not have a character.

I would structure my code like so:

game.Players.PlayerAdded:Connect(function(player)

    --some leaderstats stuff

    wait(0.1)

	player.CharacterAdded:Connect(function(character)
		 while player.Character do
			local char = player.Character
			local hrp = char:WaitForChild("HumanoidRootPart", 10)
			
			if hrp then
				local touchingParts = hrp:GetTouchingParts() --ERROR LINE
				for i, v in pairs(touchingParts) do
					if v.Parent.Parent == workspace.Checkpoints and v.Name == "Checkpoint" then
						local checkpointNumber = v.Parent.Name
						stage.Value = checkpointNumber
					end
				end
			else
				break
			end
			wait()
	    end
	end)
end)

2 comments

0

Hmmm, it doesn't seem to be working. Keep in mind also, this script has to run constantly to always check if they touch a checkpoint. I don't see the point in the break for that reason.

0

I tried doing a print directly in the while loop, it didn't print

umm i actually recommend using the Touched event of a Humanoid.. it fires whenever one of the character limbs is touched..it's a lot efficient, and easy to implement

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local character  = player.Character or player.CharacterAdded:Wait();
	local humanoid = character:WaitForChild("Humanoid");

	humanoid.Touched:Connect(function(part, limb)
		if(part.Parent.Parent == workspace.Checkpoints ) then
			--change the stage here.
		end
	end)
end)

add if statements and some sort of state indicators to avoid repetition of the same task.. as that can be a real hurdle to performance

**edit: **the above has a fatal flaw that i didn't catch when initially writing the answer.. here is the functional code

local Players = game:GetService("Players")

function OnHumanoidTouched(otherpart, limb)
	if(otherpart.Parent.Parent == workspace.Checkpoints)  then
		local player = game:GetPlayerFromCharacter(limb.Parent)
		--change the stage here
	end
end

function OnCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid");
	humanoid.Touched:Connect(OnHumanoidTouched)
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(OnCharacterAdded)
end)

4 comments

0

Ok. It seems to not work after the player dies.

0

Fixed it. Also, I can't use .Touched because of this "Glitch."

0

Edited the OP

This discussion is preserved as it was. New answers, comments, and votes are closed.