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

Script works in studio but not in a regular server?

Asked by 6 years ago

The code below checks if a players UserId is the same as the one in the table and if they type !Admin in the chat its meant to make a UI visible or invisible depending on its state.

This works in studio but doesn't in a regular server? How would I make it work in a regular server?

--- Turning Admin UI on and Off

local admins = {

    21063444;

}

local function IsAdmin(Player)
    for _,Admin in pairs(admins) do
        if type(Admin) == "number" and Admin == Player.UserId then
            return true
        end
    end
end

game.Players.PlayerAdded:Connect(function(Player)
    Player.Chatted:Connect(function(message, recipient)
        if not recipient and IsAdmin(Player) then
            if message == "!Admin" then 
                if Player.PlayerGui.Console.Main.Visible == true then
                    Player.PlayerGui.Console.Main.Visible = false
                elseif Player.PlayerGui.Console.Main.Visible == false then
                    Player.PlayerGui.Console.Main.Visible = true
                end
            end
        end
    end)

end)

0
I assume the script is located in workspace? sonicstar56 33 — 6y
0
The script is located in serverscriptservice Fraxinus_Sanctus 38 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago

The problem with this script is that the script is working on the server and attempting to make changes to the client (which includes UI).

This cannot happen without the help of a RemoteEvent.

Here is what your script should look like:

local admins = {

    21063444;
    -1; --For testing inside of Studio

}

local function IsAdmin(Player)
    for _,Admin in pairs(admins) do
        if type(Admin) == "number" and Admin == Player.UserId then
            return true
        end
    end
end

game.Players.PlayerAdded:Connect(function(Player)
    Player.Chatted:Connect(function(message, recipient)
        if not recipient and IsAdmin(Player) then
            if message == "!Admin" then 
                game.ReplicatedStorage.Toggle:FireClient(Player)
            end
        end
    end)

end)

Find ReplicatedStorage and place a RemoteEvent in there, and name it "Toggle".

Next, create a LocalScript inside of Main. This is what it should look like:

game.ReplicatedStorage.Toggle.OnClientEvent:Connect(function()
    if script.Parent.Visible == true then
        script.Parent.Visible = false
    elseif script.Parent.Visible == false then
        script.Parent.Visible = true
    end
end)

The reasoning behind this is that when then RemoteEvent is fired (line 20 of the first script), a LocalScript on the client can receive the signal and create a reaction (basically the whole LocalScript) since LocalScripts can make changes on the client and regular scripts cannot.

0
You do not need those if statements on the last script. You can simply say script.Parent.Visible = not script.Parent.Visible User#19524 175 — 6y
Ad

Answer this question