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

Players are not being damaged while they are standing still, ways to fix this?

script.Parent.Touched:Connect(function(Toucher)

local Player = game.Players:GetPlayerFromCharacter(Toucher.Parent)

if Player then

local Character = Player.Character

if Character then

Character.Humanoid.Health = Character.Humanoid.Health - 5

end

end

end)

I have this code to detect if a player touches it, it's gonna hurt em for 5 damage. However, it doesn't work while they are standing still, what can i do to make it so it hurts them whenever?

1 comment

0

loop it

2 answers

Well, "Toucher" can be used to get the character. You don't need to get the character from game:GetService("Players").LocalPlayer.Character. Just use Toucher, you're using unnecessary code to get something that can be easily received with less code being used.

 local Part = script.Parent --This is the part we'll be using to damage the player.
Part.Touched:Connect(function(touchedPart) --Everytime our 		part is touched, we'll use touchedPart as said part that touched the parent of this script.
	local Humanoid = touchedPart.Parent:WaitForChild("Humanoid") --We'll find the humanoid.
	if Humanoid then --If the humanoid exists we continue the code
		Humanoid:TakeDamage(5) --We damage the humanoid.
	end
end)

2 comments

0

This is useful, however this does not solve my problem where the players don't get damaged while they stand still.

0

try doing a while loop, like while standingOnBrick == true do

ur code is so unnecessary its way easier to do this make another script in the same part and name it GiveDamage in the original script u do

debounce = false

script.Parent.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then--if it find the humanoid then

if hit.Parent:FindFirstChild("GiveDamage") then--if there already is a script called givedamage then

script.Parent.GiveDamage.Parent = script.Parent--give damage.Parent = the scripts parent

elseif not hit.Parent:FindFirstChild("GiveDamage") then--if it cant find the script givedamage then

if not debounce then

debounce = true

script.Parent.GiveDamage:Clone().Parent = hit.Parent--it gives u a clone of the script

print("script given")

wait(2)

debounce = false

end

end

end

end)

  

script.Parent.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then--if it find the humanoid then

hit.Parent.Health.Disabled = true--ur health script gets disabled

end

end)

in the give damage script u do

while true do--this will be in a loop

wait(1)--this is the timer between every script action

if script.Parent:FindFirstChild("Humanoid") then--if it find the humanoid then

script.Parent.Humanoid.Health = script.Parent.Humanoid.Health - 2--the humanoids health goes down with 2

print("lost health")

end

end

i pulled this from my own game where i have pretty much the same thing

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