Ad
Log in to vote
0

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

Asked by 17 days ago
Edited 17 days 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 17 days ago
Edited 17 days 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 419 — 17d
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 2 — 16d
Ad
Log in to vote
0
Answered by 16 days 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 14 days 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