Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

1 answer

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);

This discussion is preserved as it was. New answers, comments, and votes are closed.