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

how do i stop .Touched from spamming?

Asked by 4 years ago

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?

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. Benbebop 1049 — 4y
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? ahsan666666666666 264 — 4y
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 :) Sulu710 142 — 4y

2 answers

Log in to vote
2
Answered by
compUcomp 417 Moderation Voter
4 years ago

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.

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? ahsan666666666666 264 — 4y
0
Glad I could help :D My Discord is Inanimate Object#8673 compUcomp 417 — 4y
Ad
Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

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
i know i could have but i diddnt for the very reason it would only damage one person. ahsan666666666666 264 — 4y

Answer this question