I tried to make skill event stop work if player get hit while using skill but after this event doesn’t work
Code:
local e
e = event.OnServerEvent:Connect(function(player)
--(THE SKILL CODE)
char:GetAttributeChangedSignal("Hitted"):Connect(function()
if char:GetAttribute("Hitted") == true then
e:Disconnect()
sound:Destroy()
particles:Destroy()
m6d:Destroy()
end
end)
end)
After i disconnect the event it doesn’t work when i try to fire it again
Based on the answer from this post, and what @FroDev1002 said, once you disconnected it won’t work anymore – it will work only once – unless you connect it again. Disconnecting an event means to make it stop working!
Try giving more context on what you need. Why does the skill a player is using need to stop when the player is hit? Does he die? Detailing your objectives gives us more information to work on and help you better.
Like i said i tried to make skill stop work after player got hit, he doesn’t die, i’m making it for much quality of game when player got hit i disconneting the event for it doesn’t go further and deleting the particles,sound,etc of that skill i want to it disconnects event like separate not the all event
How is that looks:
Perhaps if you don’t disconnect it but leave all the other pieces of the code, would that work?
Something like:
e = event.OnServerEvent:Connect(function(player)
--(THE SKILL CODE)
char:GetAttributeChangedSignal("Hitted"):Connect(function()
if char:GetAttribute("Hitted") == true then
--e:Disconnect()
sound:Destroy()
particles:Destroy()
m6d:Destroy()
return -- This will stop any following code from running, while keeping what you're doing of removing the particles etc.
end
end)
end)
By the way wouldn’t char:GetAttributeChangedSignal still fire as the event still exists, it just can’t be run through the remote event anymore but it will still run if the attribute changes.
So you should also disconnect the attributechangedsignal event too.
And even if it doesn’t it would just be better for optimization imo.
@xXxEsportsxXx, you could try adding changing your code to be like this. So the Event will still exist and Fire, but it will NOT run when char:GetAttribute("Hitted") is true.
local e
e = event.OnServerEvent:Connect(function(player)
if char:GetAttribute("Hitted") == true then return end
--(THE SKILL CODE)
end)
char:GetAttributeChangedSignal("Hitted"):Connect(function()
if char:GetAttribute("Hitted") == true then
sound:Destroy()
particles:Destroy()
m6d:Destroy()
end
end)
I’m also putting the Connection that destroys the Sound and Particles into a separate line. Otherwise, we risk Connection Leaking (meaning multiple Connections identical to that one would run at once, which isn’t good).
I agree, might not be the best approach, but I would do something like:
local function skill(player)
--(THE SKILL CODE)
end
local e
e = event.OnServerEvent:Connect(skill)
char:GetAttributeChangedSignal("Hitted"):Connect(function()
if char:GetAttribute("Hitted") == true then
e:Disconnect() -- Disconnect
-- Clear everything
sound:Destroy()
particles:Destroy()
m6d:Destroy()
-- Maybe wait some time or add some debounce logic
e = event.OnServerEvent:Connect(skill) -- Reconnect
end
end)
But if he doesn’t add any debounce or waiting, I don’t see a point in disconnecting the event, since it would be redundant
@SirTobiii and @Ransomwavee, great ideas but I think you guys forgot that OP probably wants the attributechangedsignal not to run either after the remote event is triggered so it would be a good idea to disconnect the attribute event aswell since it would be taking up memory for useless reasons.
I don’t believe he should disconnect :GetAttributeChangedSignal since he probably changed the attribute back to false after the player was hit, so he would have to reconnect the event again. I don’t think it’s worth the hassle of handling that for no clear reason, and he will need to keep handling being hit until the player dies.
local function SkillFunc(player)
local char = player.Character or player.CharacterAdded:Wait()
if char:GetAttribute("Hitted") == true then return end
--(Skill code particles sounds hitbox etc)
player.Character:GetAttributeChangedSignal("Hitted"):Connect(function()
if player.Character:GetAttribute("Hitted") == true then
breaker = true
sound:Destroy()
particles:Destroy()
m6d:Destroy()
SkillFunc():Disconnect()
end
end)
end
event.OnServerEvent:Connect(function(player)
SkillFunc(player)
end)
You Don’t wrap all of that inside a function. Remove everything related to SkillFunc and just use the method I gave you. You’re getting confused and mixing everything together.