Script returns string as nil

Local:

local Tool = script.Parent

local DrinkSound = script.Parent:WaitForChild("DrinkSound")

local db = false

local AddEffect = game.ReplicatedStorage.RemoteEvents:WaitForChild("MysteriousPotionAddEffect")

local Contents = script.Parent:WaitForChild("Contents")

local Effects = {
	["SpeedBoost"] = 70;
	["JumpBoost"] = 70;
	["Heal Player"] = 35;
	["LowerGravity"] = 12;
	["Damage Player"] = 5;
}

local function ChooseEffect()
	
	local Sum = 0
	
	for effect, chance in pairs(Effects) do
		
		Sum += chance
		
	end
	
	local RandomNum = math.random(Sum)
	
	for effect, chance in pairs(Effects) do

		if RandomNum <= chance then
			
			return effect
			
		else
			
			RandomNum -= chance
			
		end

	end
	
end

Tool.Activated:Connect(function()
	
	if db == false then
		
		local ChosenEffect = ChooseEffect
		
		AddEffect:FireServer(ChosenEffect)
		
	end
	
end)

local t = 3

while wait(0.03) do
	
	local hue = tick() % t / t
	
	local color = Color3.fromHSV(hue,1,1)
	
	Contents.Color = color
	
end

Server:

-- This script handles gadget purchases and gadgets effects

local AddEffect = game.ReplicatedStorage.RemoteEvents:WaitForChild("MysteriousPotionAddEffect")

local EffectsDetails = {
	["SpeedBoost"] = {0.35,20}; -- Amount in %, Duration
	["JumpBoost"] = {0.35,20};
	["Heal Player"] = {0.35};
	["LowerGravity"] = {0.7,20};
	["Damage Player"] = {0.15};
}

local Animation = script:WaitForChild("MysteriousPotionAnimation")

AddEffect.OnServerEvent:Connect(function(player, ChosenEffect)
	
	print(ChosenEffect)
	
	local Char = player.Character
	
	local Hum = Char:WaitForChild("Humanoid")
	
	local AnimationToPlay = Hum:LoadAnimation(Animation)
	AnimationToPlay:Play()
	
	if ChosenEffect == "SpeedBoost" then
		
		Hum.WalkSpeed += math.floor(Hum.WalkSpeed * EffectsDetails.SpeedBoost[1])
		
		wait(EffectsDetails.SpeedBoost[2])
		
		Hum.WalkSpeed = 16
		
	end
	
end)
1 Like

Does the issue start on the local script or inside the server script?

Try adding a print output before you fire the server from the local script with the output being the value that will be pushed up to the server, i see in your code you already are printing on the server side, so see what the client is trying to pass up

it starts at the quoted line above

local ChosenEffect = Effects[math.random(1,#Effects)]

But I have chances so I cannot do that

1 Like

I found something that I find odd:

local RandomNum = math.random(Sum)

That random num will always return Sum not a random number

no it actually turns a random num i printed it

works not but I’d rather for it to work as percentages

you actually do, when you store the return

if it returns as nil, its fired
just it hasnt found it

wdym dont fire?

extra text awyadhwaodnao

local ChosenEffect = ChooseEffect

Should be

local ChosenEffect = ChooseEffect() --for the function, it must have the () since it is not a variable

when you make this:

local ChosenEffect = ChooseEffect()

you fire the function

He didn’t write the () , he wanted the function?

true sjñdgh jdkf.ghahfxghfhggfhxh

There isn’t any other variable so i guessed that wait for his reply

replace this:

	local ChosenEffect = ChooseEffect

by this:

	local ChosenEffect = ChooseEffect()

works!

thank you

aidawidhgaidhaod

Your welcome!

Chars things nvm them