Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

player.Chatted() function not working?

Asked by 2 years ago

So I was making a verification-kinda thing in my obby game that when you guessed the correct code (random each server) in the chat you will teleport to the next level, and when you guessed it wrong you will die, and start over. But the script is not working, I tried multiple ways to fix it and it's still not working. I tried putting print functions to the script and there are no prints in the dev console. Here is the code:

local notDEBOUNCE = game.ReplicatedStorage.THETRUEANSWER.Value

while wait() do
    notDEBOUNCE = game.ReplicatedStorage.THETRUEANSWER.Value
end

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        msg = string.lower(msg)
        if msg == (string.lower(tostring(notDEBOUNCE))) then
            if plr.SENDVERIFICATION.Value == true and plr.SENDVERIFICATION2.Value == true then
                plr.SENDVERIFICATION.Value = false
                plr.SENDVERIFICATION2.Value = true
                plr.leaderstats.Stage.Value = 51
                plr.Character.Humanoid.Health = 0
                print("success!!")
            end
        else
            if plr.SENDVERIFICATION.Value == true and plr.SENDVERIFICATION2.Value == true then
                plr.SENDVERIFICATION.Value = false
                plr.SENDVERIFICATION2.Value = true
                plr.leaderstats.Stage.Value = 50
                plr.Character.Humanoid.Health = 0
                print("wrong!!")
            end
        end
    end)
end)
1
My man, how could you have forgotten the basic principle of a loop? Loops REPEAT code; your program is indefinitely stuck on the 4th line. Ziffixture 6913 — 2y
0
I do not see the need for that loop, either. Not to mention the name "notDEBOUNCE"...? Compare THETRUEANSWER directly. Ziffixture 6913 — 2y
0
Also I would suggest using camel case for your variable naming. It cleans things up a lot. KingDomas 153 — 2y
0
thx for the comments i didnt know loop could get stuck and repeat the whole code, im still at beginner level in coding. also i added the loop because i want the variable to update every second because it prints a blank when try to print the variable T3_MasterGamer 2189 — 2y

1 answer

Log in to vote
1
Answered by 2 years ago

Hello, it is really easy! You put while loop before it connected the events which means they will never get connected because code will never reach it. Use spawn function or better put it after these connections. And that loop is not even needed, learn more about code mechanics.

local code = game.ReplicatedStorage.THETRUEANSWER

local function onPlayerChatted(plr, msg)
   msg = string.lower(msg)
   if msg == (string.lower(tostring(code.Value))) then
       if plr.SENDVERIFICATION.Value and plr.SENDVERIFICATION2.Value then
           plr.SENDVERIFICATION.Value = false
           plr.SENDVERIFICATION2.Value = true
           plr.leaderstats.Stage.Value = 51
           plr.Character.Humanoid.Health = 0
           print("success!!")
       end
   else
       if plr.SENDVERIFICATION.Value and plr.SENDVERIFICATION2.Value then
           plr.SENDVERIFICATION.Value = false
           plr.SENDVERIFICATION2.Value = true
           plr.leaderstats.Stage.Value = 50
           plr.Character.Humanoid.Health = 0
           print("wrong!!")
       end
   end
end

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
      onPlayerChatted(plr, msg)
    end)
end)
0
oh ok i'l try it later when roblox is not down T3_MasterGamer 2189 — 2y
Ad

Answer this question