Script only working sometimes

Hi, I have scripts that utilize the Player.Chatted function and for some reason they only work sometimes and the times they don’t work, they give no error. I’ve tried in multiple games and this function only works sometimes.

This is one of the scripts:

	plr.Chatted:Connect(function(msg)
               if msg:lower() == "-ifeadmin" then
		           event:FireClient(plr,"accessifeadmin") 
end
   end
           )
           end) 

This works most of the time but sometimes it doesn’t.
Can someone please help me figure out why this is the case?

6 Likes

I’m unaware of the issue here as it should work completely fine - if this is a part of the code, then I’m assuming there’s an error above your code or under.

I would suggest posting the rest of the code (if any) and any errors that you encountered. The code will work as is, which means there’s an underlying problem that isn’t presented in your post.

1 Like

I didn’t encounter any errors. It worked sometimes, other times it didn’t. The code before it always works fine too, so I don’t think that could be a problem.

Just a general question, the code looks fine to me expect for the organization e. Are you spelling the exact word each time? I know it sounds dumb but it might be the problem.

I’ve also brought it into my studio and there is a error.
image

Here is some update code.

plr.Chatted:Connect(function(msg)
	if msg:lower() == "-ifeadmin" then
		event:FireClient(plr,"accessifeadmin") 
	end
end)

The problem was that you had a extra end for some reason, I may be wrong as you only included that part of your code but hope this helps!

Thanks for responding, the other end was for the PlayerAdded event (my bad) and yes, I’m spelling it correctly each time.

Would you be able to show what happens in the client side?

event.OnClientEvent:Connect(function(arg)
    if arg == "accessifeadmin" then
       admin:TweenPosition(UDim2.new(0.5, -250,0.5, -150))
       end
 end)

I’m not really sure what is wrong here, since you don’t have to provide all arguments for TweenPosition. Try make it so it prints something when the client is fired to check if it even responds.

The server didn’t pick it up. I made it print both on the server and client and it didn’t work on either, however, on the occasions it did work, it obviously printed.

Were the messages that you typed that were printed different from those that weren’t?

I had it print the message I sent , not even if it equaled -ifeadmin, just after the Chatted event. It didn’t even print anything.

I’m not very familiar with the lower() method, I usually just use string.lower(string) but I’m not sure if that will even help.

It doesn’t detect me chatting though. If it does detect it, everything works fine.

That is very odd, I am not sure how to fix it since it only works sometimes but somehow not all the times.

It should not behave that way. I looked at your code, and I couldn’t find any problem that could relate to what you told about.

What do you suggest I do to make it not behave this way?

1 Like

Try this and tell me if it works:

plr.Chatted:Connect(function(message, recipent)`
	local message = "-ifeadmin
	
	if string.lower(tostring(message)) == tostring(message) then
		event:FireClient(plr, "accessifadmin")
	end
end)

A big help would be to describe what “sometimes” means. If for instance you told us that when it
doesn’t work for a player, then it never works until that player rejoins the game, that would give us a
much clearer picture. If you told us that for the same player without rejoining works and sometimes
doesn’t, that would be less clear. Did you try printing all messages, to see if your cmd is indeed being
received, but possibly some other junk is in the string as well?

1 Like

Hi, when I say “sometimes” I mean it doesn’t work for any player in the game until the server resets and hopefully you get lucky. As for your second question, I made it print any message received by Player.Chatted and in the times it didn’t work, nothing was printed.