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

Kill script for obby won't work reliably, help?

I made a script to kill players that touch lava parts in an obby I'm making. However, using the .Touched event seems to have a glitch. Is there any working alternative that would be fast and efficient? 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)

Thanks!

1 comment

0

ALL I NEEDED TO DO WAS SET THE CANCOLLIDE OF THE LAVA PARTS OFF!

2 answers

✓ Accepted answer

You should defiently be checking if the lava part itself is touched rather than checking if the humanoid is touched. As well, there is a more efficient way to set up your .Touched event by getting all of the tagged objects using CollectionService and setting up the connection from there.

As for your jumping issue, this doesn't seem to be a problem in the code I provided below. If it still is an issue, there might be some other code in your game that is causing this problem to occur. GIF can be found here

Try something like this:

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

local DAMAGE_AMOUNT = 100

local function killPlayer(Hit)
	-- Let's make sure its an actual player that hit the KillBrick.
	if Players:GetPlayerFromCharacter(Hit.Parent) then
		local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
		if Humanoid then
			Humanoid:TakeDamage(DAMAGE_AMOUNT)
		end
	end	
end

local function addKillBrick(Part)
	if Part:IsA("Part") then
		-- NOTE: If the part is ever removed or destroyed, 
		-- or the tag is removed, not disconnecting this connection will cause a
		-- memory leak. 
		-- More info: https://developer.roblox.com/en-us/api-reference/function/CollectionService/GetTagged
		Part.Touched:Connect(killPlayer)
	end
end

-- Loops through all objects tagged with "KillBrick".
for _, Part in pairs(CS:GetTagged("KillBrick")) do
	addKillBrick(Part)
end

-- If a object is added with the tag after our loop is ran, connect it to the addKillBrick function.
CS:GetInstanceAddedSignal("KillBrick"):Connect(addKillBrick)

4 comments

0

It works. It still doesn't kill the player if they are jumping tho

0

The likely issue is that the .Touched is not registering fast enough while the player is jumping. You could do a few things to fix this.

0

Such as...?

0

An easy fix would just to set the kill bricks cancollide to false, at that point it'll have a chance of always registering that a player is touching it. If you don't want to follow that simple fix my other suggestion would be using Raycasting and shooting a ray under the players feet and seeing if comes in contact with any "Kill Bricks".

Theres a simpler way to do this, i think what your doing i may not be right but youve made a scipt in the player so when the player touches a lava brick they die, instead what you should do is making a simple script in the lava brick so when a person touches the brick the brick finds a humanoid and deals damage to it.

heres a simple script to do what i explained:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		local humanoid = hit.Parent.Humanoid
		local damage = math.huge
		humanoid:TakeDamage(damage)
	end
end)

just paste this script inside a lava brick. what its doing it when it gets touches it looks for a humanoid in the object that touched it. if it finds the object it deals damage to it.

hope this helped.

3 comments

0

Well, first off I'm using collectionService to tag all of the kill bricks because it would take hours to add scripts to them.

0

And I think it's a glitch with the .Touched in general not just Humanoid.Touched

0

Tested it, I'm correct. It suffers from the same issue as shown in the imgur link.

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