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)