Comparing Instance to Number Error

I am trying to make a simple random chance winner with math.random so that when the player clicks R a random number is selected and if that number is more than or equal to 56, text is printed on the server that says the player “won”, but if they lose, text is printed on the client saying they “lost”.

However. when I run the code, an error comes up saying this.


NOTE: I am purposefully using remote functions because I want to get familiar with them as a beginner scripter.

This is the code from the server script:

local rs = game:GetService("ReplicatedStorage")
local roll = rs:FindFirstChild("Rollin")
local loser = rs:FindFirstChild("rolled")

local function decidewinner(player, number)
	if number >= 56 then
		print(player.Name.." has won!")
	else 
		loser:InvokeClient(player)
	end
end

roll.OnServerInvoke = decidewinner

This is the code from the client script:

local rs = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local roll = rs:FindFirstChild("Rollin")
local rolling = rs:FindFirstChild("rolled")
local player = game.Players.LocalPlayer

if player then
	local function breakinnews(plr)
		plr = player
		print(player.Name.." has lost... Better Luck Next Time")
	end
	
	rolling.OnClientInvoke = breakinnews
	
	UIS.InputBegan:Connect(function(input, isTypin)
		if isTypin then return end
		if input.KeyCode == Enum.KeyCode.R then
			local randomnumber = math.random(1,100)
			print(player.Name.." is Rolling...")
			task.wait(2)
			roll:InvokeServer(player, randomnumber)		
		end
	end)
end

The code works fine(well doesn’t come up with any error) if I only do == and not >= or <= in the if statement in the decidewinner function.

You don’t need to send the player arg, it is automatically provided by the server.

roll:InvokeServer(randomnumber)		

Ohhh okay, well that was an easy fix… Thank you so much!

Also, how come I need to include the player argument for InvokeCilent?

Also FYI, you are generating a random number from the client and sending it to the client which is easily exploitable, instead generate the random number from the server ensuring security and saving bandwidth (cuz numbers take up a painful amount of bytes)

Think of it like this, there is only 1 server but there can be more than 1 i.e multiple clients (players). So when you are invoking the server as a client, you don’t need to mention a specific recipient cuz there is only one, whereas from the server, you do need to specify a recipient cuz there are multiple players to choose from

Ah okay, I thought it would be better to do it on the client because they wouldn’t know what number it is, but I was gravely mistaken…

Ohhh okay. That makes sense, thank you yet again!

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