Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it possible to make some line of code fire WHILE a part is being touched?

Asked by 1 year ago
Edited 1 year ago

I'm trying to make a spleef like game and in it i want an ice mechanic where you step on it and it would crack, and then it would eventually break. I use the .touched event to know when a player is touching the part. But that only fires when the player BEGINS to touch the part. I need it to fire while the player is touching and stop firing when the player has stopped. So the main question is that if its possible to make something fire while a player is touching a part? And if so, how?

3 answers

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

Use BasePart.TouchEnded event. It fires when the player untouches the part.

local isCracking = false

Part.Touched:Connect(function(otherPart) -- if the player touches the part
    isCracking = true
end)

Part.TouchEnded:Connect(function(otherPart) -- if the player untouches
    isCracking = false
    print("Player untouched the part. Part stopped cracking.")
end)

while isCracking do -- if isCracking is true, it will repeat printing every 1 second
    print("The part is cracking.")
end
0
you could do GettouchingParts() Puppynniko 1059 — 1y
0
I have figured out the solution from the person that commented on your answer. Thank you for your help and time! I have figured it out Afterineta 4 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

I figured out the solution. Instead of making the code fire while its touching. I made a part that will be invisible directly on top of the ice so that when the person is on the ice, they will be inside the part above the ice.

local ice = script.Parent
local partdetector = ice.Part


while wait(0.5) do
    for i,v in pairs(game.Workspace:GetPartsInPart(partdetector)) do
        if v.Parent:FindFirstChild("Humanoid") then
            print("theres a person on it!")
        end
    end
end

This works in detecting if a person is on the ice, also being inside the detecting part. Thank you for your help but I have found the solution thanks to puppynniko's comment recommendation.

Log in to vote
0
Answered by 1 year ago

Try using in your code within each of the iceblocks:

local ice = script.Parent
local iceDetect = ice.Detector
local iceHP = 3 -- make the ice block break in 6 seconds

while true do
    if touchDetect.touched:Connect(function()(
        iceHP = iceHP - 1 -- take away one health from ice

        print("Ice has taken damage!") -- do what you want to do

        if iceHP == 0 then
            ice:Remove() -- gets rid of ice
        end

        wait(2) -- as if it's breaking to your weight every two seconds
    end)
end

this is an inefficient way but it works.

Answer this question