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

Can I make GUI visible using server script?

Asked by 4 years ago

this is my script:

~~~~~~~~~~~~~~~~~

local Part = script.Parent

local function playerHasTool(player, toolName)
    local tool = player.Backpack:FindFirstChild(toolName) or player.Character:FindFirstChild(toolName)
 if tool then
        return true
    else
        return false
    end
end 




Part.Touched:Connect(function(HIT)
    local Player = game.Players:GetPlayerFromCharacter(HIT.Parent)
    local H  = HIT.Parent:FindFirstChild("Humanoid")

    if H and not playerHasTool(Player,"MediterraneanAvenue") then
        Player.PlayerGui.MediterraneanAvenue.StartUp.Visible = true
        end

    if H and playerHasTool(Player,"MediterraneanAvenue") and not playerHasTool(Player,"BalticAvenue") then
        Player.PlayerGui.MediterraneanAvenue.NeedToOwnAll.Visible = true
    end

    if H and playerHasTool(Player,"MediterraneanAvenue") and playerHasTool(Player,"BalticAvenue") then
        Player.PlayerGui.MediterraneanAvenue.ClickBuild.Visible = true
    end





end)


Part.TouchEnded:Connect(function(part) -- leaves the part
if game.Players:GetPlayerFromCharacter(part.Parent) then -- if the part leaving is a player
local Player = game.Players:GetPlayerFromCharacter(part.Parent) -- the player
if Player.PlayerGui.MediterraneanAvenue.StartUp.Visible == true then -- if the gui is open
    Player.PlayerGui.MediterraneanAvenue.StartUp.Visible = false
if Player.PlayerGui.MediterraneanAvenue.NeedToOwnAll.Visible == true then
    Player.PlayerGui.MediterraneanAvenue.NeedToOwnAll.Visible = false
if Player.PlayerGui.MediterraneanAvenue.ClickBuild.Visible == true then
    Player.PlayerGui.MediterraneanAvenue.ClickBuild.Visible = false
end
end

end
end
end)

~~~~~~~~~~~~~~~~~ It is for making GUI visible when a player touches a part, if he has some tools in his backpack. it is NOT A LOCAL SCRIPT but a normal script that is inside the part which has to be touched. However, when the part is touched, the GUI doesn't appear.

Can I know why? Can someone pleeeeeasssssseeee help me?

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Quantum, the best way to do this is a RemoteEvent. Put in a RemoteEvent in ReplicatedStorage and name it "OnTouch".

Server Side:

wait()
local RS = game:GetService("ReplicatedStorage")
local OnTouchEvent = RS:WaitForChild("OnTouch")
function onTouched(hit)
    local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if plr then
        OnTouchEvent:FireClient(plr)
    end
end

Local Side (Put this under every frame that you want to be visible):

wait()
local RS = game:GetService('ReplicatedStorage')
local OnTouchEvent = RS:WaitForChild("OnTouch")
local Frame = script.Parent
OnTouchEvent.OnClientEvent:Connect(function()
    if Frame.Visible == true then
        Frame.Visible = false
    end
    if Frame.Visible == false then
        Frame.Visible = true
    end
end)

if I helped, please accept this answer. If it didn't, please comment.

Ad
Log in to vote
0
Answered by 4 years ago

I finally got the answer, myself. In the respective guis I had issues with, I inserted a local script each, which directed them to become visible when the part was touched. Here, the part that had to be touched was mediterranean Avenue, so here is how my script was:

local Part = script.Parent
    local function playerHasTool(player, toolName)
        local tool = player.Backpack:FindFirstChild(toolName) or player.Character:FindFirstChild(toolName)
     if tool then
            return true
        else
            return false
        end
    end



local Gui = script.Parent
local Medpart = game.Workspace.MediterraneanAvenue

local Player
    Medpart.Touched:Connect(function(HIT)
        Player = game.Players:GetPlayerFromCharacter(HIT.Parent)
        local H  = HIT.Parent:FindFirstChild("Humanoid")
if H and  playerHasTool(Player,"MediterraneanAvenue") and not playerHasTool(Player,"BalticAvenue") then
    Gui.Visible = true
    wait(3)
    Gui.Visible = false
end
end)

I also got rid of the conditionals that had to deal with these guis in the server script, as I had already inserted a local script. It turns out that only local scripts can modify guis for local Players.

However, if you want a gui to appear to all the clients on the server, you can make use of remote events.

Hope this helps anyone in need in the future!

Answer this question