Unable to cast value to object error?


Before I begin, I swear, this is my last one! I’m still learning, so I need the explanations! Thank you.


Alright, so I’m making a little system here, and I’m trying to send something to the client via remote events. Here is my code as of now:

-- Editable Features: 

local username = "FxllenCode" -- Username of whitelisted user goes here, used for remote event.

local allowed = 82738847 -- UserID of whitelisted user goes here.

local commandStart = "!startAlerts" -- You can change this to whatever you want.

local commandEnd = "!endAlerts" -- This one too.

local sound = 1403257129 -- Set this to any audio ID you would like.



-- Do not touch the lines below unless you know what your are doing.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Players = game:GetService("Players")

local remoteEvent = ReplicatedStorage:WaitForChild("PlaySound")

local check = false

local enabled = false

local confirm = false



local cache = {}
function getUserIdFromUsername(name)
	-- First, check if the cache contains the name
	if cache[name] then return cache[name] end
	-- Second, check if the user is already connected to the server
	local player = Players:FindFirstChild(name)
	if player then
		cache[name] = player.UserId
		return player.UserId
	end 
	-- If all else fails, send a request
	local id
	pcall(function ()
		id = Players:GetUserIdFromNameAsync(name)
	end)
	cache[name] = id
	
	return id
	
	
end



	
function checkForPerms(name)
	
	local id = getUserIdFromUsername(name)
	
	if id == allowed then 
		
		check = true
		print("Sound Perms")
		print(id)
		
	else
		
		check = false
		print("No perms")
		print(id)
		
	end
	end	



function pausePerms(player)
	
	if enabled == true then
		
		
		confirm = false
		enabled = false
		
	end
	
	
end




function confirmPerms(player)
	
	if check == true then
		
		confirm = true
		
		while confirm == true do 
			
			enabled = true
			print("plsplspls")
			wait(5)
			
		end
	
		
	end
	
end


Players.PlayerAdded:Connect(function(player)
	checkForPerms(player.Name)
end)

game.Players.PlayerAdded:Connect(function(player) -- call the yes
	player.Chatted:Connect(function(message)
		if message == commandStart then
			
			confirmPerms(player)
			
			print("im not an idiot yay")
		end
	end)
end)

game.Players.PlayerAdded:Connect(function(player) -- stop calling the yes
	player.Chatted:Connect(function(message)
		if message == commandEnd then
			
			pausePerms(player)
			
			print("me not big dumb")
		end
	end)
end)


game.Players.PlayerAdded:Connect(function()
	if enabled then
		print("e")
		
		remoteEvent:FireClient(username, sound)		
		
	end
end)
	

print(check)
print(allowed)
print()

The issue is I get an error in the console that says, Unable to cast value to object, line 145. (this is where I fire the remote event). Does anyone know why this might be happening? I’ve looked at some other answers and it did not help me. If you know the answer, please make sure to explain it so I learn!

I know my code isn’t the cleanest, I’m first making it functional, then I’ll do a better re-write.

Thank you in advance!

The first argument for FireClient must be the Player Object itself.

I see this in documentation, however I do not want the event to fire for that player, it needs to be the whitelisted player. Hopefully that makes sense.

Trying this out now, cannot believe I didn’t think of that. I would still keep the username varible as a string? Just replace this code? I’ll try it, thank you!

I will try that if the above doesn’t work. Thank you so much.

As it is, the remote event will never be fired, if your intention is to fire the remote event every time enabled is true, then you should make this change

-- Editable Features: 

local username = "FxllenCode" -- Username of whitelisted user goes here, used for remote event.
local allowed = 82738847 -- UserID of whitelisted user goes here.
local commandStart = "!startAlerts" -- You can change this to whatever you want.
local commandEnd = "!endAlerts" -- This one too.
local sound = 1403257129 -- Set this to any audio ID you would like.

-- Do not touch the lines below unless you know what your are doing.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local remoteEvent = ReplicatedStorage:WaitForChild("PlaySound")
local check = false
local enabled = false
local confirm = false

local cache = {}
function getUserIdFromUsername(name)
	-- First, check if the cache contains the name
	if cache[name] then 
		return cache[name] 
	end
	-- Second, check if the user is already connected to the server
	local player = Players:FindFirstChild(name)
	if player then
		cache[name] = player.UserId
		return player.UserId
	end 
	-- If all else fails, send a request
	local id
	pcall(function ()
		id = Players:GetUserIdFromNameAsync(name)
	end)
	cache[name] = id
	
	return id
end

function checkForPerms(name)
	local id = getUserIdFromUsername(name)
	
	if id == allowed then 
		check = true
		print("Sound Perms")
		print(id)
	else
		check = false
		print("No perms")
		print(id)
		
	end
end	

-- this function was added
function onEnabled (player)
	print("e")
	remoteEvent:FireClient(player, sound)		
end

function pausePerms(player)
	if enabled == true then
		confirm = false
		enabled = false
	end
end

function confirmPerms(player)
	if check == true then
		confirm = true
		
		while confirm == true do 
			enabled = true
			onEnabled(player) -- calling when enabled is true
			print("plsplspls")
			wait(5)
		end
	end
end

Players.PlayerAdded:Connect(function(player)
	checkForPerms(player.Name)
end)

Players.PlayerAdded:Connect(function(player) -- call the yes
	player.Chatted:Connect(function(message)
		if message == commandStart then
			
			confirmPerms(player)
			
			print("im not an idiot yay")
		end
	end)
end)

Players.PlayerAdded:Connect(function(player) -- stop calling the yes
	player.Chatted:Connect(function(message)
		if message == commandEnd then
			
			pausePerms(player)
			
			print("me not big dumb")
		end
	end)
end)

print(check) -- it will always be false
print(allowed)
print()

That was not the issue, It was a string.

if you try it you will see that it no longer has that error. I simply used the player because it was within scope.

1 Like

GetPlayers returns an array, so this doesn’t work.

2 Likes

It worked as I already defined Players.