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

Unable To Cast Value To Object?

I'm redoing my Chat Commands Script (This Time including string.match), and I'm working on the J:Warn part of the script. However, whenever I try to run it, I get the error:

Unable To Cast Value To Object Any Reason Why?

Code:

local plrService = game:GetService("Players")
local plrsPermitted = require(game.ServerStorage.PlayerNamesPermitted)
local ValidCmds = require(game.ServerStorage.ValidCommands)
local WarnEvent = game.ReplicatedStorage.ToWarn
function CheckPlayersPermisson(PlayerToCheck,ModuleToSearch)
	local PlayerVar
	for _, permittedPlayer in pairs(ModuleToSearch) do
		if PlayerToCheck.Name == permittedPlayer then
			PlayerVar = PlayerToCheck
		end
	end
	if PlayerVar ~= nil then
		return true
	else
		return false
	end
end

function SearchForClosestMatch(String, CurrentTableOfPlayers)
	for _, plr in pairs(CurrentTableOfPlayers) do
		if string.match(plr.Name, "^"..String) then
			return plr
		else
			return nil
		end
	end
end

function CheckCmd(CmdString, ModuleToSearch)
	for _, validCmd in pairs(ModuleToSearch) do
		if CmdString == validCmd then
			return true
		end
	end
end

plrService.PlayerAdded:Connect(function(Player)
	local WarnsValue = Instance.new("NumberValue")
	WarnsValue.Value = 0 
	WarnsValue.Parent = Player
	WarnsValue.Name = "Warns"
	local Allowed = false
	if CheckPlayersPermisson(Player, plrsPermitted) then
		Allowed = true
	end
	Player.Chatted:Connect(function(StringMsg)
		if Allowed then
			local BrokenUpMessage = StringMsg:split(" ") --Table
			local Cmd = BrokenUpMessage[1]
			if CheckCmd(Cmd, ValidCmds) then
				if Cmd == ValidCmds[1] then
					local TargPlrAbrv = BrokenUpMessage[2]
					if SearchForClosestMatch(TargPlrAbrv, plrService:GetPlayers()) ~= nil then
						local FoundPlr = SearchForClosestMatch(TargPlrAbrv,plrService:GetPlayers())
						local Reason = StringMsg:split(TargPlrAbrv)[2]
						FoundPlr:Kick(Reason)
					else
						warn("Didn't Find Player")
					end
				end
			end
			if CheckCmd(Cmd, ValidCmds) then
				if Cmd == ValidCmds[2] then
					local TargetedPlrString = BrokenUpMessage[2]
					WarnEvent:FireClient(TargetedPlrString)
					local WarnValInTarPlr =SearchForClosestMatch(TargetedPlrString, plrService:GetPlayers()):FindFirstChild("Warns")
					if WarnValInTarPlr then
						WarnValInTarPlr.Value += 1
						if WarnValInTarPlr.Value == 3 then
							WarnValInTarPlr.Parent:Kick("Max Warns")
						end
					end
				end
			end	
		end
	end)
end)

Line That Errors:

WarnEvent:FireClient(TargetedPlrString)

1 comment

1

Well, if you want to fire to client, the first argument is the Player object whom you are firing to.

1 answer

✓ Accepted answer

Like the person said in the comments :FireClient() first paramater is the player who you wanna the even to fire for, maybe you ment to do :FireClients() which fires for every player

1 comment

0

Never mind, i found out the issue, i was trying to fire an event to a string, not an actual player, i have updated my script and all works fine now. Thanks for your answer anyway!

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