Status effect module not working properly

I’m attempting to make a status effect module for a game of mine, but the script that affects the target doesn’t work. Everything works but the actual script that affects the target.

(DebuffTest) Script

local ServerScriptService = game:GetService("ServerScriptService")
local StatusHandler = require(ServerScriptService.StatusEffects)
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	wait(5)
	StatusHandler.ApplyEffect(player.Character,"Stunned",2)
end)

(StatusEffects) ModuleScript

local StatEffect 		= {}

local StatEffectTypes = {
	Slowness = "Debuffs",
	Tiredness = "Debuffs",
	Stunned = "Debuffs",
}

function StatEffect.ApplyEffect(char : Model,StatEffect : string,Level : number)
	local CSE  = char:FindFirstChild("CurrentStatusEffects")
	if CSE:FindFirstChild(StatEffect) then Remove(char,StatEffect) end

	local effectModule = require(script[StatEffectTypes[StatEffect]][StatEffect])
	effectModule:ApplyEffect(char,Level)
end

function StatEffect.TimedApplyEffect(char : Model,StatEffect : string,Level : number, duration)
	local effectModule = require(script[StatEffectTypes[StatEffect]][StatEffect])
	effectModule:TimedApplyEffect(char,Level,duration)
end

function Remove(char : Model, StatEffect : string)
		local CSE  = char:FindFirstChild("CurrentStatusEffects")
		for i, statEffect in pairs(CSE:GetDescendants()) do
			if statEffect:IsA("Configuration") then
				-- Capturing the Status Effect that needs to be removed.
				for i, Value in pairs(statEffect:GetDescendants()) do
					if Value:IsA("BoolValue") then
						-- Deactivating the Status Effect.
						Value.Value = false
					end
				end
				-- Gives the Effect script time to revert the changes done to the Target.
				task.wait()
				statEffect:Destroy()
			end
		end
	end

function StatEffect.Remove(char : Model, StatEffect : string)
	local CSE  = char:FindFirstChild("CurrentStatusEffects")
	for i, statEffect in pairs(CSE:GetDescendants()) do
		if statEffect:IsA("Configuration") then
			-- Capturing the Status Effect that needs to be removed.
			for i, Value in pairs(statEffect:GetDescendants()) do
				if Value:IsA("BoolValue") then
					-- Deactivating the Status Effect.
					Value.Value = false
				end
			end
			-- Gives the Effect script time to revert the changes done to the Target.
			task.wait()
			statEffect:Destroy()
		end
	end
end

return StatEffect

(Stunned) Sub-ModuleScript

local module = {}

function module:ApplyEffect(char : Model,Level : number)
	local StatusEffect = script[tostring(Level)][script.Name]:Clone()
	local CSE  = char:FindFirstChild("CurrentStatusEffects")
	StatusEffect.Effect.Enabled = true
	StatusEffect.Parent = CSE
	StatusEffect.Target.Value = char
	StatusEffect.Activated.Value = true
end

function module:TimedApplyEffect(char : Model,Level : number,duration : number)
	local StatusEffect = script["Timed" ..tostring(Level)]:Clone()
	local CSE  = char:FindFirstChild("CurrentStatusEffects")
	StatusEffect.Parent = CSE
	StatusEffect.Target.Value = char
	StatusEffect.Duration.Value = duration
	StatusEffect.Activated.Value = true
end

return module

(Effect) Script

local Target = script.Parent.Target
local Activated = script.Parent.Activated

local SavedWP = 0
local SavedSS = 0

function Effect()
	local char = Target.Value
	print(char)
	local Humanoid = char:FindFirstChildWhichIsA("Humanoid")
	print(Humanoid)
	local WalkSpeed = char:GetAttribute("WalkSpeed")
	local SprintSpeed = char:GetAttribute("SprintSpeed")
	
	print(WalkSpeed)
	print(SprintSpeed)
	
	SavedWP = WalkSpeed
	SavedSS = SprintSpeed
	
	local AffectedSS = (SprintSpeed - (SprintSpeed * .5))
	local AffectedWS = (WalkSpeed - (WalkSpeed * .5))
	
	char:SetAttribute("WalkSpeed",AffectedWS)
	char:SetAttribute("SprintSpeed",AffectedSS)
end

function RevertChanges()
	local char = Target.Value
	local WalkSpeed = char:GetAttribute("WalkSpeed")
	local SprintSpeed = char:GetAttribute("SprintSpeed")
	local Humanoid = char:FindFirstChildWhichIsA("Humanoid")
	
	char:SetAttribute("WalkSpeed",SavedWP)
	char:SetAttribute("SprintSpeed",SavedSS)
end

game:GetService("RunService").Heartbeat:Connect(function()
	wait()
	local deactivated = false
	if Activated.Value == false and not deactivated then
		deactivated = true
		RevertChanges()
	end
end)

Activated.Changed:Connect(function(value)
	if value then 
		Effect()
	else
		RevertChanges()
	end
end)

Explorer

image

What have you done to debug the issue? Also, your deactivated variable is redundant

I’ve first tried to use that RunService connect function, but it didnt work (I forgot to delete it in the screenshot). Then I tried using a bindable event to try and jumpstart the script after it transferred to the target’s CurrentStatusEffects folder, and that didn’t work either.

I’ve put print functions in the activated connect function to see if there was any activity passing through, and I didn’t notice anything on the server-sided output.

Please post your code in text, instead of in screenshots. Start and end the code with ```, so that it recognizes it is code.

--It'll look like this, and much nicer for everyone

image

1 Like

Hold on I’ll update the post so my code is in text. Hope this helps!

So I managed to find a fix to this issue. I had to put task.wait() before I activated the bool value, giving the script time to properly check the boolvalue!

function module:ApplyEffect(char : Model,Level : number)
	local StatusEffect = script[tostring(Level)][script.Name]:Clone()
	local CSE  = char:FindFirstChild("CurrentStatusEffects")
	StatusEffect.Parent = CSE
	StatusEffect.Effect.Enabled = true
	StatusEffect.Target.Value = char
	task.wait()
	StatusEffect.Activated.Value = true
end