You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
so im currently working on a parry based game with different spells and status effects. in order to apply these status effects for every character (not just players but NPC’s too) i implemented this script:
local statuschecker = require(game.ServerStorage.modules.StatusManager)
local combatmanager = require(game.ServerStorage.modules.CombatManager)
local physicsservice = game:GetService('PhysicsService')
physicsservice:RegisterCollisionGroup('Entity')
physicsservice:RegisterCollisionGroup('Entity2')
physicsservice:CollisionGroupSetCollidable("Entity","Entity", false)
local function setup(c)
local h = c:FindFirstChild("Humanoid")
if h then
if c.Name == "Enemy" then return end
statuschecker:StatusCheck(c)
end
end
for i, c in pairs(workspace:GetChildren()) do
setup(c)
end
workspace.ChildAdded:Connect(function(c)
setup(c)
end)
workspace.ChildRemoved:Connect(function(c)
local h = c:FindFirstChild("Humanoid")
if h then
statuschecker:Terminate()
end
end)
it just checks if any new characters are added to the workspace and if there is one, it will use the status module scripts status checker, sorry for the messy code im new to scripting and have just been trying to fix this bug:
local rs = game:GetService("RunService")
local CombatTable = require(game.ServerStorage.modules["Combat Table"])
local playerhandler = require(game.ServerStorage.modules.PlayerHandler)
local db = false
local check
local module = {}
function module:StatusCheck(char)
local fireeffect = game.ReplicatedStorage.StatusEffects.FireParticles.FireParticlesAttachment
local windeffect = game.ReplicatedStorage.StatusEffects.wind.windattachment
local staminasubtract = Instance.new("IntValue")
staminasubtract.Parent = char
staminasubtract.Name = "staminasub"
staminasubtract.Value = 0
check = rs.Heartbeat:Connect(function()
if char == nil then return end
local p = game.Players:GetPlayerFromCharacter(char)
if char ~= nil then
local CV = CombatTable[char]
if p then
if CV.Posture >= 0 then
CV.Posture = CV.Posture - 0.002
game.ReplicatedStorage.Posture:FireClient(p,CV.Posture)
end
end
if p then
if CV.Mana < 100 then
CV.Mana = CV.Mana + 0.02
game.ReplicatedStorage.spells.Mana:FireClient(p,CV.Mana)
end
end
if CV.Shielded then
if char.Humanoid.Health < 100 then
char.Humanoid.Health = char.Humanoid.Health + 0.005
end
end
if CV.Shadow then
if p then
game.ReplicatedStorage.spells.Shadow:FireClient(p)
end
if not char.HumanoidRootPart:FindFirstChild("ShadowAttachment") then
local shadoweffect = game.ReplicatedStorage.StatusEffects.Shadow1.ShadowAttachment:Clone()
shadoweffect.Parent = char.HumanoidRootPart
game.Debris:AddItem(shadoweffect,5)
if not char:FindFirstChild("Humanoid") then
shadoweffect:Destroy()
end
end
CV.Shadow = false
end
if CV.Fire then
if CV.GettingCarried or CV.GettingGripped then
CV.Fire = false
end
if not char.HumanoidRootPart:FindFirstChild("FireParticlesAttachment") then
local fireeffectclone = game.ReplicatedStorage.StatusEffects.FireParticles.FireParticlesAttachment:Clone()
fireeffectclone.Parent = char.HumanoidRootPart
if not char:FindFirstChild("Humanoid") then
fireeffectclone:Destroy()
end
end
if not char:FindFirstChild("Humanoid") then return end
if CV.Knocked then
char.Humanoid.Health = char.Humanoid.Health - 0.001
else
char.Humanoid.Health = char.Humanoid.Health - CV.BurnDamage
end
task.delay(5,function()
if CV.Fire then
CV.Fire = false
end
end)
elseif CV.Fire == false then
if char.HumanoidRootPart:FindFirstChild("FireParticlesAttachment") then
char.HumanoidRootPart:FindFirstChild("FireParticlesAttachment"):Destroy()
end
if CV.Wind then
if not staminasubtract then return end
if not CV.Wind then return end
if not char.HumanoidRootPart:FindFirstChild("windattachment") then
local windeffectclone = windeffect:Clone()
windeffectclone.Parent = char.HumanoidRootPart
if not char:FindFirstChild("Humanoid") then
windeffectclone:Destroy()
end
staminasubtract.Value = 1
wait(3)
CV.Wind = false
windeffectclone:Destroy()
end
if not CV.Wind then
staminasubtract.Value = 0
end
end
end
end
end)
end
function module:Terminate()
check:Disconnect()
end
return module
I recently implemented a new function (the terminate one that you see in both scripts) that disconnects this heartbeat runservice loop.
-
What is the issue? Include screenshots / videos if possible!
Before adding this disconnection function, whenever a character would die, the loop would continue and push out errors over and over as there was no character and nothing to stop it. After implementing it, it worked fine until i tested it with multiple people, deaths and even just casting certain spells would cause the game to freeze and sometimes crashed the server. it probably has something to do with the terminate function i created although im not sure how else i can stop the loop.