ChatService errors because of this script and I don't know why

I have a script that checks if a player is eligible to get a donator chat tag. But the script doesn’t work and I get a weird error message from chatservice…

 lower is not a valid member of Player "Players.trueblockhead101"  -  Server - ChatService:169

while true do
	wait()
	game.Players.PlayerAdded:Connect(function(playerName)
		local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner").ChatService)
		local Speaker = ChatService:GetSpeaker(playerName)
		local player = game.Players[playerName]
		local stat = player:WaitForChild("Donator")
		if stat.Value == 1 then
	
			Speaker:SetExtraData('Tags', {{TagText = "Donator", TagColor = Color3.fromRGB(124, 252, 0)}})


		end

	end)
end

And I don’t know how to fix that or how to get my script to work.

Get rid of the loop, it’s just going to cause problems. PlayerAdded is all you need.

You’re attempting to pass a player to ChatService:GetSpeaker() which expects a string. players.PlayerAdded passes a player as the first argument. Also get rid of the loop as @JarodOfOrbiter said.

1 Like

Well I got rid of the loop yet nothing changed. And what do I do to fix what @7z99 said?
I’m a little confused right now…

Pass a string to the function, not a player.

Such as player.name?


2 Likes

Try this:

--//Services
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

--//Requires
local ChatService = require(ServerScriptService:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))

--//Functions
Players.PlayerAdded:Connect(function(player)
	local Speaker = ChatService:GetSpeaker(player.Name)
	local stat = player:WaitForChild("Donator")
	
	if stat.Value == 1 then
		Speaker:SetExtraData('Tags', {{TagText = "Donator", TagColor = Color3.fromRGB(124, 252, 0)}})
	end
end)

Also, never connect functions in a loop without disconnecting them. It will cause huge memory issues and will most likely lead to your game to crashing within minutes or even seconds. You also shouldn’t connect functions in a loop for most cases.

1 Like

Still doesn’t work… There are no errors.

Print value. Maybe they doesn’t have value for some reason, as doesn’t seems to be using Datastore.

Try this instead: (sorry, I connected the wrong function on my first post)

--//Services
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

--//Requires
local ChatService = require(ServerScriptService:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))

--//Functions
ChatService.SpeakerAdded:Connect(function(playerName)
	local Player = Players:WaitForChild(playerName)
	local stat = Player:WaitForChild("Donator")
	
	if stat.Value == 1 then
		local speaker = ChatService:GetSpeaker(playerName)
		speaker:SetExtraData('Tags', {{TagText = "Donator", TagColor = Color3.fromRGB(124, 252, 0)}})
	end
end)

Man it still doesn’t work…


Hm, that’s weird. Could you show the script where you make the Donator instance for the player?

I have the player service up and my player has the value in it. And it is set to one.

Setting by hand in Players folder?

Maybe you can move that value in script.

Could you show the script? I can’t help much by just a description.

The script that created it or the script that sets the value?

I would like to see both scripts.

Both. I thought that the script that created it would also set the value.

sets it to 1 once a player donates

local Mps = game:GetService("MarketplaceService")
local players = game.Players

local product id = 1272983101 

local function processReceipt(receiptInfo)
	local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if player then
		
		print("Bought")
		local stat = player:FindFirstChild("Donator")
		stat.Value = 1
	
	end
	
end

Mps.ProcessReceipt = processReceipt

and this is my datastore script (currently the donator value isn’t saved and i know why and I’ll fix it later)

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("playerData")

local function onPlayerJoin(player)

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

	local Point = Instance.new("IntValue")
	Point.Name = "Points"
	Point.Parent = leaderstats

	local Donator = Instance.new("IntValue")  --Here
	Donator.Name = "Donator"
	Donator.Parent = player

	local xp = Instance.new("IntValue")
	xp.Name = "Xp"
	xp.Parent = leaderstats

	local playerUserId = player.UserId
	local data = playerData:GetAsync("Player_"..playerUserId)  

	if data then
		Point.Value = data['Points']
		xp.Value = data['Xp']
		Donator.Value = data['Donator']
	else
		Point.Value = 0
		xp.Value = 0
		Donator.Value = 0
	end
end

local function onPlayerExit(player)
	local player_stats = {}
	if player then
		for i, stat in pairs(player:WaitForChild("leaderstats"):GetChildren()) do
			player_stats[stat.Name] = stat.Value
		end

		playerData:SetAsync("Player_"..player.UserId, player_stats)
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

game:BindToClose(function()
	local LastPlayer = nil
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		local player_stats = {}
		if player then
			if player ~= LastPlayer then
				for i, stat in pairs(player:WaitForChild("leaderstats"):GetChildren()) do
					player_stats[stat.Name] = stat.Value
				end
			end

			playerData:SetAsync("Player_"..player.UserId, player_stats)
		end
	end
end)

Btw: I have to go so if I don’t respond that’s why.

Okay so, my current script works, but doesn’t detect if the player buys the item. Use my current script, but also change your MarketplaceService script to this:

--//Services
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local MarketplaceService = game:GetService("MarketplaceService")

--//Requires
local ChatService = require(ServerScriptService:WaitForChild("ChatServiceRunner"):WaitForChild("ChatService"))

--//Controls
local ProductId = 1272983101

--//Functions
MarketplaceService.ProcessReceipt = function(receiptInfo)
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)

	if player then
		local stat = player.Donator
		stat.Value = 1
		
		local speaker = ChatService:GetSpeaker(player.Name)
		speaker:SetExtraData('Tags', {{TagText = "Donator", TagColor = Color3.fromRGB(124, 252, 0)}})
		
		return Enum.ProductPurchaseDecision.PurchaseGranted
	else
        return Enum.ProductPurchaseDecision.NotProcessedYet
    end
end
1 Like