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

...Touched:Connect(function(p), how to make it stop touching the thing it touched before?

so i'm making a sword;

script.Parent.Handle.Touched:Connect(function(p)
	if script.Parent.CanDamage.Value == true then
		script.Parent.CanDamage.Value = false
		local AttackDamage = 								math.random(script.Parent.MinAttackDamage.Value,script.Parent.MaxAttackDamage.Value)
		local Victim = p.Parent:FindFirstChild("Humanoid")
		if Victim == nil then
			return p
		else
		Victim:TakeDamage(AttackDamage)
		local DamageText = Instance.new("TextLabel",p.Parent.Head.DamageGui) -- Displays the damage done to the humanoid
		DamageText.Name = "DamageText"
		DamageText.Text = AttackDamage
		DamageText.Size = UDim2.new(1,0,1,0)
		DamageText.BackgroundTransparency = 1
		DamageText.TextColor3 = Color3.fromRGB(255,255,255)
		DamageText.TextScaled = true
		DamageText.TextStrokeTransparency = 0.5
		local DamageTextScript = game.ServerStorage.DamageTextScript:Clone() -- Will add the script for removing and deleting the copied DamageText
		DamageTextScript.Parent = DamageText
		DamageTextScript.Disabled = false
		wait(script.Parent.AttackDelay.Value)
		script.Parent.CanDamage.Value = true
		end
	end
end)

i'm not sure how it can change the info stored in the function(p) part because i can't get it o attack other things if the thing it attack doesn't have a humanoid. please help me...

3 comments

0

I dont understand what you're asking, you want to be able to attack things that dont have a humanoid?

0

No, he wants to make the sword to only damage an enemy once.

0

no. i want to be able to attack something that is alive even though what it touched before wasnt. therefore having the script locking on to that thing that wasnt alive previously.

1 answer

✓ Accepted answer

.Touched events will fire whenever any block of any sort touches the part you're calling the event from. For example, if you had a .Touched event on a door, it would fire for all of the parts touching it such as the floor, door frame, player arm, leg, torso etc.

Because of this, it is important to make sure that you validate your code so that it only runs when something specific touches it. There are many ways to do this;

This will check to make sure the touch-ee has a humanoid (useful for NPCs)

part.Touched:Connect(function(part)
    if part.Parent:FindFirstChild'Humanoid' then
        --// Code here
    end
end)

This will check to make sure the touch-ee is a player (useful if you only want players to be able to activate it)

part.Touched:Connect(function(part)
    local player = game:GetService'Players':GetPlayerFromCharacter(part.Parent)
    
    if player then
        --// Code here
    end
end)

Hope this helped you :)

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