I am trying to make a part where you stand on the part and whilst standing on it you will take damage the remainder of the time you are standing on it. Once the player comes off the part the damage is stopped. Here's the code;
-- When player touches part and it is true, health decreases by 10 local decrease = script.Parent local HEALTH = 100 local function onTouch(plr) print("Ouch") local player = plr.Parent local character = player:FindFirstChildWhichIsA("Humanoid") if character then character.Health = HEALTH - 10 end end decrease.Touched:Connect(onTouch)
This would help me figure out if someone could answer!
So this exposes the weakness of the Touched function
, you can use the function GetPartsInPart
instead which returns an array of parts touching the block, and loop through every part touching the block.
local playersTouching = {}--we use an array to insert the players touching the block while true do wait(1) local touchingParts = game.Workspace:GetPartsInPart(script.Parent) table.clear(playersTouching)--clear the array first because the player might not be touching the block anymore for i,v in pairs(touchingParts) do if v.Parent:FindFirstChild("Humanoid") and not table.find(playersTouching,v.Parent) then--check if the player is inside the playersTouching table already, because there will be 2 leg part of a single player inside the touchingParts array, this prevents a player being inserted twice into the playersTouching table table.insert(playersTouching,v.Parent) end end for i,v in pairs(playersTouching) do--loops through all the players touching the block to decrease their health local humanoid = v:FindFirstChild("Humanoid") humanoid.Health -=2 end end
Its been a long while since i coded in roblox so this might not be the most efficient way but this will work for your given case.