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

how do i stop .Touched from spamming?

so im making a game where it has some super powers like shooting fire balls and im having some problems with a hit function in the script. this is the hit part:

fireBall.Touched:Connect(function(hit)
        if hit:IsDescendantOf(character) or hit:FindFirstChild("ParticleEmitter") then return end
		if caster == player then
			game.ReplicatedStorage.Events.FireBall:FireServer(hit)
		end
    end)

this fires when the fireball hits a player. i want the fireball to carry on and damage everyone it touches without spamming the damage

i dont want to destroy teh fireball when it touches a player so if there is a group of people i want it to damage all of them. right now whats happening is that when it touches a player it damages them multiple times by firing many remotes. how would i stop it from doing that?

3 comments

1

Try making a table of all the characters it hits and each time it damages if what it is damaging is on the table then it does nothing.

0

i get what you mean but i dont know how i would work with tables i havent really worked with them can you help me out?

0

CompUcomp and Brandon1881 both have the right answer, add a debounce system and make the event only for the humanoid root part. Incorporate both of these answers for the best script :)

2 answers

✓ Accepted answer

It's spamming because it is firing once for each of the body parts. I would suggest that you only listen for Touched events on the HumanoidRootPart.

fireBall.Touched:Connect(function(hit)
        if hit:IsDescendantOf(character) or hit:FindFirstChild("ParticleEmitter") then return end
        if caster == player and hit.Name == "HumanoidRootPart" then
            game.ReplicatedStorage.Events.FireBall:FireServer(hit)
        end
end)

Using this solution, you might have the problem of exploiters changing the name of their HumanoidRootPart to avoid damage. This could be remedied with a routine check (every minute or so maybe) on every player's character to ensure nothing is out of the ordinary.

If you want the fireball to also damage when hitting other body parts, I would suggest you give each player a hitbox that stretches over their entire character, then listen for touched events on that.

2 comments

0

ahh i understand now thanks for your help! also the fireball is now working just fine. if i need further help with other stuff can i contact you on like discord or something?

0

Glad I could help :D My Discord is Inanimate Object#8673

You should add a debounce for example:

local debounce = false

fireBall.Touched:Connect(function(hit)
        if hit:IsDescendantOf(character) or hit:FindFirstChild("ParticleEmitter") then return end
        if caster == player and not debounce then
		debounce = true
            	game.ReplicatedStorage.Events.FireBall:FireServer(hit)
        end
end)

However, this would make the fireball only hit once (to one player).

So if you wanted to make the fireball only damage people once, you would most likely have to create a table of players and insert the player name into it (when the player is hit), and only hit people who's name isnt in the table

1 comment

1

i know i could have but i diddnt for the very reason it would only damage one person.

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