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

FireClient not working inside ServerScriptService?

Asked by 3 years ago
Edited 3 years ago

I have two scripts. A local one and a server one The server one is inside the ServerScriptService The client one is inside the StarterGui.

--server script. its not the entire script.
player.Chatted:Connect(function(text)
            print(dummyCount)
            local chattext = string.lower(text)
            if chattext == "/createdummy" or chattext == "/cd" then
                if dummyCount < 4 then
                    local clonedDummy = dummy:Clone()
                    clonedDummy.Parent = folderPerPlayer
                    clonedDummy.Name = player.Name.." Dummy "..dummyCount
                    clonedDummy:MoveTo(humanoidRootPart.Position + Vector3.new(math.random(-10, 13), 7, math.random(-9, 12)))
                elseif dummyCount == 4 then
                    print(player.Name)
                    getWarning:FireClient(player, "(You can only see this!) You can't add anymore dummies! Use /removedummy (/rd) to delete all dummies!") --it wont fire
                end
            end

<><><><><><><><><><><><><><><><><>

--client script, full script. wont print message.
local starterGui = game:GetService("StarterGui")
local replicatedStorage = game:GetService("ReplicatedStorage")
local getWarning = replicatedStorage:WaitForChild("getWarning")

local function CreateSystemMessage(message, color, font, fontsize)
    starterGui:SetCore("ChatMakeSystemMessage",{
        Text = message;
        Color = color;
        Font = font;
        FontSize = fontsize;
    })
end

while true do
    CreateSystemMessage(
        "{System} Chat /createdummy (/cd) to spawn dummies and /removedummy (/rd) to remove dummies.",
        Color3.fromRGB(252, 165, 3), 
        Enum.Font.FredokaOne,
        56
    )
    wait(420)
end

getWarning.OnClientEvent:Connect(function(message)
    print(message) -- wont print?
end)

--[[
CreateSystemMessage(
    message,
    Color3.fromRGB(255, 115, 115),
    Enum.Font.FredokaOne,
    56
)
]]

1 answer

Log in to vote
1
Answered by 3 years ago

your OnClientEvent is never reached due to loop

There's the problem with only the client script, the server script is actually working fine and it's not the cause. Your client script is using loop which mean that the OnClientEvent event will never listen because OnClientEvent is never reached.

The solution is to separate the local script into 2 different local scripts so that loop is on 1st local script while OnClientEvent is on 2nd local script.

==== SOLUTION ====

==== 1st LocalScript ====

local starterGui = game:GetService("StarterGui")

local function CreateSystemMessage(message, color, font, fontsize)
    starterGui:SetCore("ChatMakeSystemMessage",{
        Text = message;
        Color = color;
        Font = font;
        FontSize = fontsize;
    })
end

while true do
    CreateSystemMessage(
        "{System} Chat /createdummy (/cd) to spawn dummies and /removedummy (/rd) to remove dummies.",
        Color3.fromRGB(252, 165, 3), 
        Enum.Font.FredokaOne,
        56
    )
    wait(420)
end

--[[
CreateSystemMessage(
    message,
    Color3.fromRGB(255, 115, 115),
    Enum.Font.FredokaOne,
    56
)
]]

==== 2nd LocalScript ====

local replicatedStorage = game:GetService("ReplicatedStorage")
local getWarning = replicatedStorage:WaitForChild("getWarning")

getWarning.OnClientEvent:Connect(function(message)
    print(message) -- now print because the event is reachable
end)

In the future, you may want to make sure that the loop isn't stopping the event from listening because the loop typically stops other code lines outside the loop from being activated.

Happy Scripting!

0
If you found this answer helpful then please accept this answer and possibly help others who may look for the same answer. Somone_exe 224 — 3y
0
Oh god I'm so dumb. Thank you so much <3 antvvnio 40 — 3y
Ad

Answer this question