Chat.Chatted doesn't work

CS.Changed:Connect(function(msg) -- (CS = ChatService)
	if msg == TheCharacter.Value then
		door.Transparency = 0.75
		door.CanCollide = false
		task.wait(4)
		door.Transparency = 0
		door.CanCollide = true
	end
end)

This script doesn’t work. I’m trying to see if the msg that the player chatted is the same as TheCharacter Value.

The reason I suspect it’s not working is because the place where this is, is inside of ReplicatedStorage and doesn’t get pulled out till later in the game.

So Players.PlayerAdded:Connect(function() end) (to get the player) wouldn’t work because scripts don’t run in ReplicatedStorage.

How do I fix this?

Any help is appreciated!

1 Like

If your script is in ReplicatedStorage then just move it to ServerScriptService. Or I misunderstood?

1 Like

Well my game is kinda like a normal elevator game, and all the floors are stored in replicated storage.

And the scripts that make the floors have stuff happen are inside the models, (which are inside replicated storage)

So when the models are pulled out from RS, and put into workspace, the scripts are activated.

(hope that clarifies things)

Try to go through all the players and make this connection for them (or else check the distance from the player to the elevator and then activate it)

1 Like

I think the problem here is the script isn’t executing once it’s cloned, which is what I think you’re doing. So you might have to re-parent the script to the model of the floors AFTER they’re moved from the ReplicatedStorage to the workspace.

Or you can move that script to its proper places, like the local script gets played into StarterPlayerScripts, StarterCharacterScripts or StarterGui and the server scripts should be in the ServerScriptService.

2 Likes

This is also one of the options, but then if each elevator has its own value, it will have to do something like a module or somehow determine what needs to be done

2 Likes

Actually, that’s a smarter way of doing it, I believe. What if you instead make a ModuleScript that has separate functions/code for the client and server, and you just have the script execute those functions inside the script? Throwing some ideas here.

1 Like

Well I tried to just put it in workspace at first, and that didn’t work.
So I think it has to do with the CS.Chatted function.

Do you think you know how I would fix this?

I already have a module script to randomize, clone etc. the floors, but I already have a lot of floors so changing all of them would be a huge task.

You’re using CS.Changed not CS.Chatted.

.Changed sends out an event when one of their properties gets changed.
.Chatted should be obvious, but sends out an event when a player sends a message.

2 Likes

It won’t work! You need to connect an event to player

1 Like

bruh, it was a typo all along.

But I did fix it and it didn’t seem to work, but I saw that it fires when CS:Chat() is fired. So how do I see when a player chats, without using PlayerAdded and using a server script?

You can use RemoteEvent to get info from client to server?

Iterate over the players if you need to make exceptions and connect them to .Chatted

for _, v in pairs(game.Players:GetPlayers()) do
	v.Chatted:Connect(function(msg, recipient)
		--code
	end)
end

Or use RemoveEvent as @zamont124 said, but that again depends on your other code and it’s a mystery to know what works best for you.

Seems like it should give you the instance that it was chatted from.
image

You need to change the function parameters from what you have to best fit the above screenshot.

-- basically from this
CS.Chatted:Connect(function(msg)
    ...
end)

-- to this
CS.Chatted:Connect(function(part, msg, _ --[[you might not need the chat color]])
    ...
end)

And from the part, you just get the character from finding the FirstAncestorOfClass(“Model”) from that part.

Could you try and demonstrate that in a script? I don’t understand the “And from the part, you just get the character from finding the FirstAncestorOfClass(“Model”) from that part.”

Basically:

local ChatService = game:GetService("ChatService")
local Players = game:GetService("Player") -- to check if the character can be found from the player

CS.Chatted:Connect(function(part, msg, _)
	local character = part:IsA("BasePart") and part:FindFirstAncestorOfClass("Model") or part 
	-- we do FindFirstAncestorOfClass("Model") to find the model that the part is a child/descendant of.
	-- this is what you'd want to do to find the player's character when you're only presented with the part.

	local player = character and Players:GetPlayerFromCharacter(character) or nil -- if the characte exists, check if there's a player that's attached to this character. 
	-- if not, or there isn't a player attached to it, return nil

	if character and player then
		-- your previous code goes here. but, for this example,
		print(Player.Name, Player.UserId, character)
	end
end
1 Like
CS.Chatted:Connect(function(part, msg, _)
	local character = part:IsA("BasePart") and part:FindFirstAncestorOfClass("Model") or part 
	local player = character and Players:GetPlayerFromCharacter(character) or nil

	if character and player then
		if msg == TheCharacter.Value then
			door.Transparency = 0.75
			door.Decal.Transparency = 0.75
			door.CanCollide = false
			task.wait(4)
			door.Transparency = 0
			door.Decal.Transparency = 0
			door.CanCollide = true
		else
			print("Wasn't the corrent msg")
		end
	end
end)

This is what I put, and nothing worked.
The print didn’t print either.

If TheCharacter is a model, then you’d want to do if character == TheCharacter.Value.

1 Like

No, TheCharacter is a string. It’s what the player needs to chat.

1 Like