Event fires twice or three times

This is a phone messaging script

local FireEventDeb = false
-- Sending
script.Parent.FocusLost:Connect(function()
    if FireEventDeb == true then return end
    FireEventDeb = true

    local From = templates:WaitForChild("FromMessagesTemplate"):Clone()
    From.Text = script.Parent.Text
    From.Parent = script.Parent.Parent:WaitForChild("Recieve")
    From.Visible = true

    game:GetService("ReplicatedStorage").SendMessage:FireServer(game.Players.LocalPlayer.Name, script.Parent.Text, script.Parent.Parent.Name)
    task.wait(1)
    FireEventDeb = false
end)

-- Recieving
local RecieveEventDeb = false

game:GetService("ReplicatedStorage"):WaitForChild("RecieveMessage").OnClientEvent:Connect(function(From, Message)
   if RecieveEventDeb == true then return end
   RecieveEventDeb = true
    local To = templates:WaitForChild("FromMessagesTemplate"):Clone()
    To.BackgroundColor3 = Color3.fromRGB(25, 25, 25)
    To.Text = Message
    To.Parent = game.Players.LocalPlayer.PlayerGui.Phone.Frame.Phone.Main.PlayerContacts:FindFirstChild(From).Recieve
    To.Visible = true
    
    task.wait(1)
    RecieveEventDeb = false
end)

Server

-- Send to reciepient
RS:WaitForChild("SendMessage").OnServerEvent:Connect(function(Player, From, Message, To)
    local Plr = game:GetService("Players"):FindFirstChild(To)
    RS:WaitForChild("RecieveMessage"):FireClient(Plr, From, Message)
end)


2 Likes

Why you are using FocusLost for this?

Seems that it could fire when player leaves the textBox, so its a chance that multiple calls for the .SendMessage:FireServer() are happening.

Perhaps a “Send Message” button would be useful?

2 Likes

I always use focuslost, whats wrong with this?

1 Like

Nothing wrong with it. I was just saying that with a Send button you can be sure that the “message” ServerRemoteListener is only triggered once and you can close the FireEventDeb when that happens. You said that event is firing multiple times, I guess

1 Like

Okay, well do you know a fix? Would enterPressed work aswell. I want the player to also use the textbox to send msgs other then the button

1 Like

Tried this it still fired the message twice.

1 Like

hey…? you there can you see the issue or not because i did the button part and it still sent twice

1 Like

Where is the local script located?

1 Like

Sorry, but I dont know why your remote SendMessage or your remote RecieveMessage are firing twice. Should be a wrong setup on your side that we dont know…

Or maybe, because you are cloning the template when sending message and when receiving the message too, and you are confusing that both are from both events.

I could check your script later if I have some free time.

And try to keep a single post for each issue you are facing, no need to duplicate posts…:

It clones once,
FromMessageTemplate is local for the sender to see he sent a message.

On the recieving event
FromMessageTemplate is turned dark grey to replicated recieving a message from someone

Narrowed down.
Coming from the player recieving the message.

When you print the message sent after the click , its one string Therefore there wasnt double clicks.

When you print the message recieved in the server , its one string Therefore the event wasnt fired twice

When you print the message the player recieves on client script , its two strings.

The code:

game:GetService("ReplicatedStorage"):WaitForChild("RecieveMessage").OnClientEvent:Connect(function(From, Message)
    if Deb == true then return end
    Deb = true
    local To = templates:WaitForChild("RecievingMessage"):Clone()
    To.Text = Message
    To.Parent = game.Players.LocalPlayer.PlayerGui.Phone.Frame.Phone.Main.PlayerContacts:FindFirstChild(string.lower(From.Name)).Recieve
    To.Visible = true
    
    print(To.Text)
    
    -- Don't mind this, this is for indicating player got sent a message
    if game.Players.LocalPlayer.PlayerGui.Phone.Frame.Phone.Main.PlayerContacts:FindFirstChild(string.lower(From.Name)).Visible == false then
        game.Players.LocalPlayer.PlayerGui.Phone.Frame.Phone.Main.Messaging.Contacts:FindFirstChild(string.lower(From.Name)).User.TextColor3 = Color3.fromRGB(255, 42, 46)
       game.Players.LocalPlayer.PlayerGui.Phone.Frame.Phone.Main.PlayerContacts:FindFirstChild(string.lower(From.Name)):GetPropertyChangedSignal("Visible"):Connect(function()
            if game.Players.LocalPlayer.PlayerGui.Phone.Frame.Phone.Main.PlayerContacts:FindFirstChild(string.lower(From.Name)).Visible == true then
                game.Players.LocalPlayer.PlayerGui.Phone.Frame.Phone.Main.Messaging.Contacts:FindFirstChild(string.lower(From.Name)).User.TextColor3 = Color3.fromRGB(255,255,255)
            end
       end)
    end
    --
    task.wait(2)
    Deb = false
end)
--Server script if needed

RS:WaitForChild("SendMessage").OnServerEvent:Connect(function(Player, From, Message, To)
    if Message == "" then
        return
    end

    local messageObject = getTextObject(Message, From.UserId)
    if not messageObject then
        return
    end

    local filteredMessage = getFilteredMessage(messageObject, From.UserId)
    RS:WaitForChild("RecieveMessage"):FireClient(game:GetService("Players"):FindFirstChild(To), From, filteredMessage)
    print(filteredMessage .. "server")
end)

This was a solution because I realized the recieving script was under the direct messages of the player thats being sent the message. Where the template duplicated twice. One for the player, one for the recievingp layer

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.