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!
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)
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.