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

local GUIs when clicked on a player?

Asked by 8 years ago

So I was wondering how I would make it so whenever I clicked on another player, I could see their stats (for example their kills, but I would implement this later). So I was thinking maybe I would add a click detector then a billboard GUI inside the player? (so maybe add it in when they spawn?) also how would I make it local, so when I click it no one else will see it. Any help would be very appreciated. ~Thanks, Bubs

1 answer

Log in to vote
1
Answered by
Pixxels 45
8 years ago
Edited 8 years ago

I would personally not use click detectors. They're deprecated, and there're much nicer methods :)

First of all, we should make a reference to the mouse:

local mouse = game.Players.LocalPlayer:GetMouse()

Then we set up a function that's fired whenever the player clicks:

mouse.Button1Down:connect(function()
    -- Clicked
end)

Then we can see what the player's mouse is currently hovering over by using the mouse.Target method, then we check if the mouse target is a body part of a player's character:

local target = mouse.Target
if target ~= nil and game.Players:FindFirstChild(target.Parent.Name) then
    -- Hovering over a player
end

(We also make sure target ~= nil to avoid any errors)

We can now get the player we're hovering over and for example display their name.

local player = target.Parent
print(player.Name)

All together thus far:

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Down:connect(function()
    -- Clicked
    local target = mouse.Target
    if target ~= nil and game.Players:FindFirstChild(target.Parent.Name) then
        -- Hovering over a player
        local player = target.Parent -- (note that this is the player's character)
        print(player.Name.." was clicked on!")
    end
end)

Great, so now that we can print out the player's name that we're looking over we should start working on the Billboard GUI that you wanted. You'll start by designing the entire billboard gui as you want it to look (using placeholder names and values for their stats). Then you parent the gui to the StarterGui. We will now simply set the adornee of the gui to the player's body to display it over them!

Let's start by setting up a reference to the gui at the beginning of our script.

local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("BillboardGui")

Now all we need to do is set the adornee and the the names/values once you click on a player:

gui.Adornee = player.Head
gui.Frame.PlayerName.Text = player.Name
gui.Frame.PlayerKills.Text = game.Players:FindFirstChild(player.Name).leaderstats.Kills.Value

All in all it should look something like this:

local mouse = game.Players.LocalPlayer:GetMouse()
local gui = game.Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("BillboardGui")

mouse.Button1Down:connect(function()
    -- Clicked
    local target = mouse.Target
    if target ~= nil and game.Players:FindFirstChild(target.Parent.Name) then
        -- Hovering over a player
        local player = target.Parent -- (note that this is the player's character)
        print(player.Name.." was clicked on!")
        -- Setting up the gui!
        gui.Enabled = true
        gui.Adornee = player.Head
        gui.Frame.PlayerName.Text = player.Name
        gui.Frame.PlayerKills.Text = game.Players:FindFirstChild(player.Name).leaderstats.Kills.Value
    else
        -- Hide the gui if none is being inspected (target wasn't a player)
        gui.Adornee = nil
        gui.Enabled = false
    end
end)

For the sake of the example I just assumed that you used a leaderboard with the "Kills".

0
yes i understood very clear and will implement this, thank you so much that was an amazing answer! Bubbles5610 217 — 8y
Ad

Answer this question