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

How to make a damage script that only damages the player if there character is moving? [closed]

Asked by 4 years ago

I'm trying to make a damage script where if the player moves on a part, it does damage but only when the character is standing still on the part will it not hurt them, any help would be great.

Closed as Not Constructive by Ziffixture and Fifkee

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
0
Answered by
0b1w 13
4 years ago
Edited 4 years ago

This ought to work m8.

local Players = game:GetService("Players");

local Part; -- Define the part

local Touching = {};

local Delay = 1.5;
local Damage = 5;

local function getHumanoid(part)
    if (Players:GetPlayerFromCharacter(part.Parent)) then
        return part.Parent.Humanoid;
    elseif (Players:GetPlayerFromCharacter(part.Parent.Parent)) then
        return part.Parent.Parent.Humanoid;
    end;
    return nil;
end;

local function partTouched(part)
    local Humanoid = getHumanoid(part);
    if (Humanoid) then
        local Player = Players:GetPlayerFromCharacter(Humanoid.Parent);
        if (not Touching[Player]) then
            Touching[Player] = true;
            while (Humanoid.Parent ~= nil and Touching[Player]) do wait(Delay);
                if (Humanoid.MoveDirection ~= Vector3.new(0,0,0)) then
                    Humanoid:TakeDamage(Damage);
                end;
            end;
        end;
    end;
end;

local function partTouchEnded(part)
    local Humanoid = getHumanoid(part);
    if (Humanoid) then
        local Player = Players:GetPlayerFromCharacter(Humanoid.Parent);
        Touching[Player] = nil;
    end;
end;

Part.Touched:Connect(partTouched);
Ad