Error code: "Unable to cast value to object"

Have a script that includes data, if the player has none it is set to false, but it gives me the error “Unable to cast value to object” on line 22

script (Server)

local Players = game:GetService("Players")
local RP = game:FindService("ReplicatedStorage")
local Event = RP.CheckingIfInAir

local RemoteFunction = RP.RemoteFunction


Event.OnServerEvent:Connect(function(player)
	print("3")
	local DataManager = require(game:GetService("ReplicatedStorage").DataManager)
	local data = DataManager:Get(player)
if data then
	local TPoseEquipped = data.TPoseEquipped
	local TPoseOwned = data.TPoseOwned
	local GoldenHookEquipped = data.GoldenHookEquipped
	local GoldenHookOwned = data.GoldenHookOwned

	RemoteFunction:InvokeClient(data, TPoseEquipped, TPoseOwned, GoldenHookEquipped, GoldenHookOwned)
		
else
	
		RemoteFunction:InvokeClient(false, false, false, false, false) --Line 22

   end
end)

and if it helps this is the client the function is being sent to.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Humanoid.Parent:WaitForChild("HumanoidRootPart")


local RP = game:FindService("ReplicatedStorage")
local Event = RP.CheckingIfInAir
local RemoteFunction = RP.RemoteFunction
-----------Animations---------------------

--//Ani 1 (Defualt)//--
local Animation = script.DefualtSwing
local AnimationTrack = Humanoid:LoadAnimation(Animation)
--//Ani 2 (T-Pose)//--
local Animation2 = script.TPoseSwing
local Animation2Track = Humanoid:LoadAnimation(Animation2)
--// Ani 3 (UpsideDown)//--
local Animation3 = script.UpsideDown
local Animation3Track = Humanoid:LoadAnimation(Animation3)
-------------------------------------------

local RopeAmount = Player:WaitForChild("NumberOfHooks"):WaitForChild("Amount")

local EventDB = false

while true do
if	Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
	print("1")
	Event:FireServer()
	wait(.5)
	end	
	wait(.5)
end


RemoteFunction.OnClientInvoke = function(player,data,TPoseEquipped,TPoseOwned,GoldenHookEquipped,GoldenHookOwned)
	print("2")
	if Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
		if RopeAmount.Value == 1 then
			local char = Player.Character

			if data then

				if TPoseEquipped.Value == true then
					Animation2Track:Play()
					Animation2Track.Looped = true
				elseif TPoseEquipped.Value == false then
					AnimationTrack:Play()
					AnimationTrack.Looped = true
				end 
			
			else 
				
				AnimationTrack:Play()
				AnimationTrack.Looped = true
				
			end
			
		elseif RopeAmount.Value == 0 then

			if data then

				if TPoseEquipped.Value == true then
					Animation2Track:Stop()
				elseif TPoseEquipped.Value == false then
					AnimationTrack:Stop()
				end

			else 
				
				AnimationTrack:Stop()  
				
			end 
		end
	end
end

Line 11
local data = DataManager:Get(player)
Is this a BoolValue?

Are all 4 of the other items in line 22 BoolValues as well?

The data is not a bool value, it can only be nil or not nil, but it says I cant use nil as a parameter.

Can you set data = nil in the line before line 22?

Yes but now it says “Argument 1 missing or nil”

I believe you need to specify which client to invoke which is causing the error as its data instead of player.

Try doing InvokeClient(player, data, …otherstuffhere)

Everything works now, no errors, buuuuuuuut. Print(“2”) aka the on client invoke part of the client script does not run :frowning:.

You should wrap the while loop in a task.spawn, it is preventing the rest of the script from running.

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