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

How can I convert this script to work with a GUI click?

Asked by 7 years ago
Edited 7 years ago

Hi there, so I've got this script that spawns a GUI on click, and needs to work with FE. But I tried simply putting the GUI into StarterGUI, and that doesn't work with FE. Could anyone please help me make this script work as a GUI Button? Note: IT NEEDS TO USE THE GUIGIVE EVENT!

script.Parent.Touched:connect(function(hit)
    if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
        game.ReplicatedStorage.GuiGive:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent),game.ReplicatedStorage,script.Name)
    end
end)

ServerScript that is needed to equip the gear!

local g,flash
function Assign(p, Value, Type) 
    local Player=p.Character    

    if Type=="Uniform Change"or Type=="Uniform Gear Change"or Type=="Gear" then
        if p.PlayerGui:FindFirstChild("Uniform Change") or p.PlayerGui:FindFirstChild("Uniform Gear Change") or p.PlayerGui:FindFirstChild("Gear")then return end       
        Value[Type]:Clone().Parent=p.PlayerGui
    end

    if Type=="Shirt" or Type=="Pants" then
    local pID = Value:findFirstChild("ID") -- Location of the ID Value
    local ID = pID.Value -- Value of ID
    local baseID = "rbxassetid://"
    local thing=Instance.new(Type)
        if Player:findFirstChild(Type) == nil then
            if Type=="Shirt" then thing.ShirtTemplate=baseID..tostring(ID-1) elseif Type=="Pants" then thing.PantsTemplate=baseID..tostring(ID-1)end
            thing.Parent=Player
            thing.Name = Type
        else
            if Type=="Shirt" then thing.ShirtTemplate=baseID..tostring(ID-1) elseif Type=="Pants" then thing.PantsTemplate=baseID..tostring(ID-1)end
            thing.Parent = Player
            local t = Player:findFirstChild(Type)
            t:remove()
            thing.Name = Type
            end 
    end

    if Player:findFirstChild("Humanoid") and (Type=="Gear" and not Player:findFirstChild("Gear")) or (Type=="Hat" and not Player:findFirstChild("Face")) then       
        if Type=="Hat" then g=Value:FindFirstChild("Face"):Clone() elseif Type=="Gear" then g=Value:FindFirstChild("Gear"):Clone() end
        g.Parent = Player
        local C = g:GetChildren()       
        for i=1, #C do
            if C[i].className == "Part" or "Unionoperation" then
                local W = Instance.new("Weld")
                W.Part0 = g.Middle
                W.Part1 = C[i]
                local CJ = CFrame.new(g.Middle.Position)
                local C0 = g.Middle.CFrame:inverse()*CJ
                local C1 = C[i].CFrame:inverse()*CJ
                W.C0 = C0
                W.C1 = C1
                W.Parent = g.Middle
                g.Middle.Transparency = 1
            end
                local Y = Instance.new("Weld")
                if Type=="Hat" then Y.Part0 = Player.Head elseif Type=="Gear" then Y.Part0 = Player.Torso end
                Y.Part1 = g.Middle
                Y.C0 = CFrame.new(0, 0, 0)
                Y.Parent = Y.Part0
        end

        local h = g:GetChildren()
        for i = 1, # h do
            h[i].Anchored = false
            h[i].CanCollide = false
        end
        if Type=="Hat" then flash=Value:FindFirstChild("RemoveH"):Clone() elseif Type=="Gear" then flash=Value:FindFirstChild("RemoveG"):Clone() end
        if Type=="Hat" then flash.Frame.RH.Hat.Disabled=false elseif Type=="Gear" then flash.Frame.RG.Gear.Disabled=false end 
        flash.Parent = p.PlayerGui      

    end
end

function game.ReplicatedStorage.Order.OnServerInvoke(p,val,typ)
    Assign(p,val,typ)
end


0
It needs a click detector. hiimgoodpack 2009 — 7y
0
@himgoodpack I don't want it to be inside a part, I want this to be INSIDE my GUI's button. Macoryx 0 — 7y
0
If you put something in StarterGUI, players will only see it after they respawn. GoldenPhysics 474 — 7y

3 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago
local ButtonToBeClicked = script.Parent
local GUIRetrieved = game.ReplicatedStorage.GiveGui

ButtonToBeClick.MouseButton1Click:connect(function()
    local New = GUIRetrieved:Clone()
    new.Parent = game:GetService("Players").LocalPlayer.StarterGui
end)
0
Will not work as it needs to use the GuiGive event to spawn the GUI. Macoryx 0 — 7y
0
StarterGui isn't located in the LocalPlayer, so it will return an error. PyccknnXakep 1225 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Remote Events are not needed, put this in a LocalScript.

script.Parent.MouseButton1Click:Connect(function()
    local gui = game:GetService("ReplicatedStorage"):WaitForChild("ScreenGui"):Clone() --whatever your GUIs name is
    gui.Parent = game:GetService("Players").LocalPlayer.PlayerGui --NOT StarterGui as it will return nil.
end)

Please accept my answer if this helped!

0
script.Parent.MouseButton1Click:connect(function() print("Clicked") game.ReplicatedStorage.GuiGive:FireClient(game.Players.Macoryx.Character,game.ReplicatedStorage,script.Name) end) Macoryx 0 — 7y
Log in to vote
0
Answered by
NecoBoss 194
7 years ago

Okay so you are going to want to put every single tool into replicated or server storage. Next you want a local script, a script, and a remote event all as children somewhere in the GUI, preferably at a level where you can use the same local script for every text button.

Why do we need a local script and a regular script? Well with filtering enabled, local scripts cannot see serverstorage or workspace, and a few other services. Consequently, regular scripts can't communicate and change things like playergui. This tutorial, wiki.roblox.com/index.php?title=Remote_Events_and_Functions, explains it way better than me.

So the first thing we need to do is create a simple function that detects if the button is clicked, and then tells the remote event that the button was clicked.

-- Local Script Code --

event = script.Parent:WaitForChild("RemoteEvent") -- in this case the scripts parent is a text button

script.Parent.MouseButton1Click:connect(function() -- anonymous function detects mouse click
    -- you can insert client side code here (any code that manipulates gui, or anything inside the local player)
    event:FireServer() -- This tells the event to literally trigger the server script function
end)

So now we want our regular (server) script:

--Regular Script Code--

local event = script.Parent:WaitForChild("RemoteEvent")  -- in this case this scripts parent is the exact same text button as the local script.

script.Parent.RemoteEvent.OnServerEvent:connect(function(player)
    game.ReplicatedStorage.Tool:Clone().Parent = player.Backpack -- You can also store the tool in server storage and I recommend you do.
end)

This is your bare bones, basic framework for your GUI. Hope this helps. Feel free to ask me any questions.

Answer this question