Script returning nil

Hi so I’m trying to make a script that fires an event and returns a value that is true or false but it ends up returning nil

Script

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, Cost, Level)
	print(Level)
	if plr.leaderstats.Money.Value >= Cost then
		plr.leaderstats.Money.Value = plr.leaderstats.Money.Value - Cost
		return true
	else
		return false
	end
end)

Local Script

script.Parent.MouseButton1Click:Connect(function()
	local result = game.ReplicatedStorage.RemoteEvent:FireServer(script.Parent.Cost.Value, script.Parent.Upgrade.Value + 1)
	print(result)
	if result == true then
		script.Parent.Cost.Value = script.Parent.Cost.Value * 2
		script.Parent.Upgrade.Value = script.Parent.Upgrade.Value + 1
		
	end
end)
1 Like

remote events don’t return any value, it’s the remote functions which do so…

RemoteEvents are one-way communication tool and does not return anything upon firing
You can use RemoteFunction instead.

Remote events dont return anything. If you want a return invoke a remote function.

In your case I would replace

game.ReplicatedStorage.RemoteEvent:FireServer(script.Parent.Cost.Value, script.Parent.Upgrade.Value + 1)

with

game.ReplicatedStorage.RemoteEvent:InvokeServer(script.Parent.Cost.Value, script.Parent.Upgrade.Value + 1)

And I would replace

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, Cost, Level)
	print(Level)
	if plr.leaderstats.Money.Value >= Cost then
		plr.leaderstats.Money.Value = plr.leaderstats.Money.Value - Cost
		return true
	else
		return false
	end
end)

with

function game.ReplicatedStorage.RemoteEvent.OnServerInvoke(plr, Cost, Level)
	print(Level)
	if plr.leaderstats.Money.Value >= Cost then
		plr.leaderstats.Money.Value = plr.leaderstats.Money.Value - Cost
		return true
	else
		return false
	end
end

Also make sure you replace the remote event with a remote function.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.