Sorry about the horrible title but I couldn't think of one that describes my problem.
I have this script that updates chat boards around the map. It checks if the player is within the global chat range and if not doesn't update the chat boards.
It works fine but once the player dies it updates the global boards no matter where they are. This also happens when their character is reloaded.
01 | game.Players.PlayerAdded:connect( function (player) |
02 | player.CharacterAdded:connect( function (char) |
03 | player.Chatted:connect( function (msg, recipient, plr) |
04 | local micpos = workspace.StageMic.Handle.Position |
05 | local charpos = char.Torso.Position |
06 | if (micpos - charpos).magnitude < = 5 then |
07 | for i,v in pairs (workspace.MessageBoardSystem:GetChildren()) do |
08 | if v:IsA( "Part" ) then |
09 | if v:FindFirstChild( "SurfaceGui" ) then |
10 | v.SurfaceGui.TextBox.Text = player.Name.. ": " ..msg |
11 | end |
12 | end |
13 | end |
14 | end |
15 | end ) |
16 | end ) |
17 | end ) |
I suspect that the problem is that you haven't disconnected the event once the player died. Try this out
01 | game.Players.PlayerAdded:connect( function (player) |
02 | player.CharacterAdded:connect( function (char) |
03 | local con 1 |
04 | con 1 = player.Chatted:connect( function (msg, recipient, plr) |
05 | local micpos = workspace.StageMic.Handle.Position |
06 | local charpos = char.Torso.Position |
07 | if (micpos - charpos).magnitude < = 5 then |
08 | for i,v in pairs (workspace.MessageBoardSystem:GetChildren()) do |
09 | if v:IsA( "Part" ) and v:FindFirstChild( "SurfaceGui" ) then |
10 | v.SurfaceGui.TextBox.Text = player.Name.. ": " ..msg |
11 | end |
12 | end |
13 | end |
14 | end ) |
15 | char.Humanoid.Died:connect( function () |
16 | con 1 :disconnect() |
17 | end ) |
18 | end ) |
19 | end ) |