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

What's an efficient way to detect whether or not a player is touching a block?

Asked by 10 years ago

I'm working on a heal power up that heals a player to max health, rests for thirty seconds, and then it reenables itself to be used again. I'm using the method that Urist McSpark implemented in one of his Hour of Code videos, where the script is finding the parent of whatever part touched it. If the parent is a player, then it finds its "Humanoid" and resets the health back to max. The problem that I ran into while testing this is that the mesh of the hat keeps touching the power up first, and its parent is the hat itself (Which is obviously not a player). Below I'll put the code I've written:

local medkit = script.Parent
local da = medkit.DecalA
local db = medkit.DecalB
local cooldown = 30
local isOn = true


local function turnoff()
    medkit.Transparency = .7
    da.Transparency = .7
    db.Transparency = .7
    isOn = false
end

local function turnOn()
    medkit.Transparency = 0
    da.Transparency = 0
    db.Transparency = 0
    isOn = true
end

local function heal(part)
    if (isOn == false) then return end
    if (isOn == true) then
    local parent = part.Parent
    local health = parent.Humanoid.Health
        if game.Players:GetPlayerFromCharacter(parent)then
            parent.Humanoid.Health = parent.Humanoid.MaxHealth
            turnoff()
        end
    end
end

medkit.Touched:connect(heal)

while true do
    wait(cooldown)
    if (isOn == false) then turnOn() end
end

As I asked in the title, how can I detect players in a better way? I apologize if it's an obvious question, as I've just picked up scripting last week. I feel like the answer is obvious, but I'm not picking up on it. Thanks in advance.

Answer this question