Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Attempt to compare userdata with number?

Even though I added .Value to the end of the variable, it still pops up that error.

The error is on the line 25. GemsNeeded is a number value.

wait(1)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EventsFolder = ReplicatedStorage:WaitForChild("EventsFolder")
local Event = EventsFolder:WaitForChild("SendExchangeRequest")

for _,Player in pairs(game.Players:GetChildren()) do
	wait()
	
	local function RequestApproved(Material, MatAmount)
		
		local Material_Value = Player:WaitForChild("Database"):WaitForChild(Material.."_Value")
		Material_Value.Value = Material_Value.Value + MatAmount
		
		-- End
	end

	local function RequestCheck(GemsNeeded, Material, MatAmount)
		
	local Gem_Value = Player:WaitForChild("Database"):WaitForChild("Value_Inv"):WaitForChild("Gem_Value")
		
	print("Checking if the user has enough Gems!")
	
	wait()
	
		if Gem_Value.Value >= GemsNeeded then
			
			-- Taking a needed gems value num from player gem value.
			print("Request Valid!")
			Gem_Value.Value = Gem_Value.Value - GemsNeeded
			
			-- Extraction to the RequestApproved funct.
			
			RequestApproved(Material, MatAmount)
			
		end
	end
	
	-- This is where the server receives a request
		Event.OnServerEvent:Connect(function(GemsNeeded, Material, MatAmount)
			RequestCheck(GemsNeeded, Material, MatAmount)
		end)
end

2 answers

In addition to "Gem_Value" needing to be "Gem_Value.Value"

This section of the code refers to a Remote Event

Event.OnServerEvent:Connect(function(GemsNeeded, Material, MatAmount)
	RequestCheck(GemsNeeded, Material, MatAmount)
end)

However, the first parameter of the Remote Event's OnServerEvent is always the player, so it should read:

Event.OnServerEvent:Connect(function(player, GemsNeeded, Material, MatAmount)
	RequestCheck(GemsNeeded, Material, MatAmount)
end)

In your previous script, the "GemsNeeded" was considered to be the player, rather than a value

Since this is a Server Script, defining a table of players at the start of the server will not allow the players to progress correctly after one time, so remove the for-loop lines

for _,Player in pairs(game.Players:GetChildren()) do
end

and then use the player parameter in the onserverevent to define the player in a local function

Revisions (Server-Side)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EventsFolder = ReplicatedStorage:WaitForChild("EventsFolder")
local Event = EventsFolder:WaitForChild("SendExchangeRequest")

local function RequestApproved(player, Material, MatAmount)
	local Material_Value = player:WaitForChild("Database"):WaitForChild(Material.."_Value")
	Material_Value.Value = Material_Value.Value + MatAmount
	-- End
end

local function RequestCheck(player, GemsNeeded, Material, MatAmount)
	local Gem_Value = player:WaitForChild("Database"):WaitForChild("Value_Inv"):WaitForChild("Gem_Value")
	print("Checking if the user has enough Gems!")
	wait()

	if Gem_Value.Value >= GemsNeeded then
		-- Taking a needed gems value num from player gem value.
		print("Request Valid!")
		Gem_Value.Value = Gem_Value.Value - GemsNeeded
		-- Extraction to the RequestApproved funct.
		RequestApproved(player, Material, MatAmount)
	end
end

-- This is where the server receives a request
Event.OnServerEvent:Connect(function(player, GemsNeeded, Material, MatAmount)
	RequestCheck(player, GemsNeeded, Material, MatAmount)
end)

MINOR TERM EDIT: "Expert" Scripter's Orders

2 comments

0

"Calling tables" that's not gonna work unless your table has a metatable with a __call metamethod pls fix terminology

0

I've tried it now and it keeps saying the same error...

You also need to do GemsNeeded.Value:

if Gem_,Value.Value >= GemsNeeded then
    -- do stuff
end

2 comments

0

Gem_Value is the name of the value inside database

0

And the GemsNeeded is a number located in local script (not a value) which is sent to this script as a variable GemsNeeded

This discussion is preserved as it was. New answers, comments, and votes are closed.