Effect randomizer problem

I am currently making a weapon (tool) for my combat game. In the tool, there is a randomizer value (which is used by 'math.random') that when the randomized number reaches a certain number, the perk activates and an effect happens on top of the damaged enemy. The problem is: when I use the tool and the number reaches the exact number, the effect won’t play. I think it has to do with the damage script (server script inside tool) instead of the tool script (Local script that plays anims and other things) because the dmg script is the one that recieves the command.

Here’s the script snippets:

local Debris = game:GetService("Debris")
local TweenService = game:GetService("TweenService")
local PerkValue = script.Parent.PERK_RANDOMIZER_VALUE.Value

script.Parent.Damage.OnServerEvent:Connect(function(Player, Character)
	local Humanoid = Character:FindFirstChild("Humanoid")
	Humanoid:TakeDamage(10)
	if Humanoid.Health <= 0 then
		Player.leaderstats.Points.Value = Player.leaderstats.Points.Value + 1
	end
	if PerkValue == 7 then
		print("Function Called!")
	    local KnockoutStar = game.ReplicatedStorage.Effects.Star:Clone()
	    local Head = Character:FindFirstChild("Head")
	    local tweenInfo = TweenInfo.new(0.75, Enum.EasingStyle.Linear)
	    local Properties = {}
	    Properties.Size = KnockoutStar.Size * 2
	    Properties.Transparency = 1
	    local Tween = TweenService:Create(KnockoutStar, tweenInfo, Properties)
	    KnockoutStar.CFrame = Head.CFrame:ToWorldSpace(CFrame.new(0, 1.5, 0)) * CFrame.Angles(math.rad(-90), 0, 0)
	    Tween:Play()
	    Tween.Completed:Connect(function()
		    Debris:AddItem(KnockoutStar, 0.7)
	    end)
	end
end)

--ToolScript (The script that assigns a value to the perk number--
local PerkRandomNumber = 1
local PerkValue = script.Parent.PERK_RANDOMIZER_VALUE.Value

PerkValue = PerkRandomNumber

So is there a solution to my problem? Please let me know as soon as possible! :smiley:

One issue I notice is that you’re declaring PerkValue at the top of your script, it’s not doing what you think it does. The PerkValue variable is assigned to whatever the value is at the time of the declaration, it’s not a reference to the Value property. If you want to get the current value, you need to get it from the function that’s being connected to OnServerEvent.

local CurrentPerk = script.Parent.PERK_RANDOMIZER_VALUE

script.Parent.Damage.OnServerEvent:Connect(function (Player, Character)
    local PerkValue = CurrentPerk.Value

Setting up your code this way makes it so that CurrentPerk is a reference to your ValueObject and PerkValue is instead defined as whatever the value of your ValueObject is at the time of the function being called. Therefore, changes will be registered when made.

Second issue is how the value is being assigned, not enough information there. Just checking: the math.random and assignment of PERK_RANDOMIZER_VALUE’s value, are they done from a Script or a LocalScript? If it’s from a LocalScript, the server won’t see that change and so you will need to randomise it from a server script. If it’s not, then fixing that first issue should be enough.