Unable To Cast Value To Object

This doesnt usually happen but whenever the Cool1 RemoteEvent goes off it gives me the error Unable to cast value to object

–Heres The Script

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Currency")

local function Setup(player)
	local playerId = player.UserId

	local LeaderStats = Instance.new("Folder")
	LeaderStats.Name = "leaderstats"
	LeaderStats.Parent = player

	local cash = Instance.new("IntValue")
	cash.Name = "cash"
	cash.Parent = LeaderStats
	

	local function updateCash(amount)
		cash.Value += amount
		game.ReplicatedStorage.Cool1:FireClient(amount)
	end


	local data
	local success, errorMsg
	for i = 1, 5 do
		success, errorMsg = pcall(function()
			data = DataStore:GetAsync("player_" .. player.UserId)
		end)
		if success then
			cash.Value = data or 0
			updateCash(cash.Value)
			break
		else
			warn(errorMsg)
		end
	end
	
	

	game.ReplicatedStorage.Option1_CashSend.Event:Connect(function() updateCash(10) end)
	game.ReplicatedStorage.Option2_CashSend.Event:Connect(function() updateCash(15) end)
	game.ReplicatedStorage.Option3_CashSend.Event:Connect(function() updateCash(25) end)
	game.ReplicatedStorage.Option4_CashSend.Event:Connect(function() updateCash(15) end)

	if player:FindFirstChild("PlayerGui") and player.PlayerGui:FindFirstChild("Currency") then
		player.PlayerGui.Currency.TextLabel.Text = tostring(cash.Value)
	end
end

local function Set(player)
	local success, errorMsg
	for i = 1, 5 do
		success, errorMsg = pcall(function()
			DataStore:SetAsync("player_" .. player.UserId, player:WaitForChild("leaderstats").cash.Value)
		end)
		if success then
			print("Success")
			break
		else
			warn(errorMsg)
		end
	end
end

game.Players.PlayerAdded:Connect(Setup)
game.Players.PlayerRemoving:Connect(Set)

FireClient has a player arg pass in the player that you want to fire to.

RemoteEvent:FireClient(player, ...any)
1 Like

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