Attemp To Call a nil value

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

make a virus creator

  1. What is the issue? Include screenshots / videos if possible!

when i put the function on remote event the server can’t receive my function and return void

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

yes

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Client

Use.MouseButton1Click:Connect(function()
	local Func = GetFuncVirus(Character) -- GetFuncVirus returns the virus function
	game.ReplicatedStorage.PlayVirus:FireServer(Func,Character)
end)

Server

game.ReplicatedStogare.PlayVirus.OnServerEvent:Connect(function(Player,Func,Character)
    Func(Character) -- error: attemp to call a nil value
end)

note: i used print on client and returns the function not nil

here are the complete code:

Client

local Stages = script.Parent.Stages
local Kill = Stages.Kill
local Wait = Stages.Wait
local VirusDo = script.Parent.VirusDo
local Use = script.Parent.Use
local SpecialWait = Stages.SpecialWait
local Infect = Stages.Infect
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

function GetFuncVirus(Character)
	return function()
			for i,v in pairs(VirusDo:GetChildren()) do
				if v:IsA("TextButton") then
					if v.Name == "Kill" then
						Character.Health = 0
					elseif v.Name == "Wait" then
						wait(1)
					elseif v.Name == "SpecialWait" then
						wait(v.Time.Value)
					elseif v.Name == "Infect" then
					local InfectRange = 10
					local Closestcharacter,Closestdistance = nil,math.huge
					for i,v in pairs(workspace:GetChildren()) do
						if v:FindFirstChild("Humanoid") then
							local Distance = (v.PrimaryPart.Position-script.Parent.Position).Magnitude
							if Distance <= InfectRange then
								game.ReplicatedStorage.PlayVirus:FireServer(GetFuncVirus(v),v)
							end
						end
					end
				end
			end
		end
	end
end

Kill.MouseButton1Click:Connect(function()
	local KillClone = Kill:Clone()
	KillClone.Parent = VirusDo
end)
Wait.MouseButton1Click:Connect(function()
	local WaitClone = Wait:Clone()
	WaitClone.Parent = VirusDo
end)
SpecialWait.MouseButton1Click:Connect(function()
	local Clone = SpecialWait:Clone()
	local Time = Instance.new("NumberValue")
	Time.Name = "Time"
	Time.Value = (tonumber(script.Parent.SpecialText.Text) or 1)
	Time.Parent = Clone
	Clone.Parent = VirusDo	
end)
Infect.MouseButton1Click:Connect(function()
	local Clone = Infect:Clone()
	Clone.Parent = VirusDo
end)
Use.MouseButton1Click:Connect(function()
	local Func = GetFuncVirus(Character)
	print(Func)
	game.ReplicatedStorage.PlayVirus:FireServer(Func,Character)
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Where is the continuation of this elseif statement?

If you wanna check whether v is a player, do game:GetService("Players"):GetPlayerFromCharacter(v).

I assume script.Parent is a GuiObject. You should use Character.PrimaryPart.Position instead.

I noticed your GetFuncVirus() function doesn’t use the Character parameter. I think that’s why it said nil?

no

Does it print anything here???

no

image

I meant the local script, not the server script.

oh ok

image

yes

Seems like it does print something… give me a second.

I see, it seems like you are not doing any returning inside your GetFuncVirus().

wait no

function GetFuncVirus(Character)
	return function() -- yes return
			for i,v in pairs(VirusDo:GetChildren()) do
				if v:IsA("TextButton") then
					if v.Name == "Kill" then
						Character.Health = 0
					elseif v.Name == "Wait" then
						wait(1)
					elseif v.Name == "SpecialWait" then
						wait(v.Time.Value)
					elseif v.Name == "Infect" then
					local InfectRange = 10
					local Closestcharacter,Closestdistance = nil,math.huge
					for i,v in pairs(workspace:GetChildren()) do
						if v:FindFirstChild("Humanoid") then
							local Distance = (v.PrimaryPart.Position-script.Parent.Position).Magnitude
							if Distance <= InfectRange then
								game.ReplicatedStorage.PlayVirus:FireServer(GetFuncVirus(v),v)
							end
						end
					end
				end
			end
		end
	end
end

yes, but inside the function, you didn’t return anything.

how?

image

I think the print statement is printing out the hex locatin of the function, not what’s being returned inside the function you made:

function() -- yes return
			for i,v in pairs(VirusDo:GetChildren()) do
				if v:IsA("TextButton") then
					if v.Name == "Kill" then
						Character.Health = 0
					elseif v.Name == "Wait" then
						wait(1)
					elseif v.Name == "SpecialWait" then
						wait(v.Time.Value)
					elseif v.Name == "Infect" then
					local InfectRange = 10
					local Closestcharacter,Closestdistance = nil,math.huge
					for i,v in pairs(workspace:GetChildren()) do
						if v:FindFirstChild("Humanoid") then
							local Distance = (v.PrimaryPart.Position-script.Parent.Position).Magnitude
							if Distance <= InfectRange then
								game.ReplicatedStorage.PlayVirus:FireServer(GetFuncVirus(v),v)
							end
						end
					end
				end
			end
		end
	end

Do you see any return inside this function?

1 Like

the important not is return, the important it’s the server run the function

Can you confirm if this line fires using print statements?

i’m run the code first on

Use.MouseButton1Click:Connect(function() -- first
	local Func = GetFuncVirus(Character)
	print(Func)
	game.ReplicatedStorage.PlayVirus:FireServer(Func,Character)
end)

not on the infection

elseif v.Name == "Infect" then -- on the infection
					local InfectRange = 10
					local Closestcharacter,Closestdistance = nil,math.huge
					for i,v in pairs(workspace:GetChildren()) do
						if v:FindFirstChild("Humanoid") then
							local Distance = (v.PrimaryPart.Position-script.Parent.Position).Magnitude
							if Distance <= InfectRange then
								game.ReplicatedStorage.PlayVirus:FireServer(GetFuncVirus(v),v)
							end
						end
					end

the player can select if the virus can infect players or not then not very important

answer me please :frowning_face: