Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I get the player leaderstats with this script?

Here in the script, when I click on a player the TextLabel and Image becomes a value of the leaderstats, but the problem is that the local hum = target.Parent is looking for the player in the game.workspace, how do I get the local hum = target.Parent to go to game.Players? Is that leaderstats are in game.Players and not game.workspace

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local sc = script.Parent.Holder
local selection = Instance.new("SelectionBox")
selection.Color3 = Color3.fromRGB(255, 0, 0)
selection.Parent = script

mouse.Button2Down:connect(function()
local target = mouse.Target

    if not target then
        selection.Adornee = nil
    else
            local hum = target
            if hum ~= nil then
                    sc.Player.Image = "https://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&userId="..hum.UserId --PlayerID
                    sc.Attack.Text = "Attack: "..hum.leaderstats.Attack.Value --leaderstats
                    sc.Agility.Text = "Agility: "..hum.leaderstats.Aggility.Value --leaderstats
                    sc.Defense.Text = "Defense: "..hum.leaderstats.Defense.Value --leaderstats
                    sc.Ki.Text = "Ki: "..hum.leaderstats.Energy.Value --leaderstats
                    sc.Username.Text = ""..hum.Name --PlayerName
        end
    end
end)

Error line 16 userid is not a valid member of part

5 comments

0

try mouse.Button2Down:connect(function(target) target = mouse.Target

0

Also it should just be hum = target not target.Parent, target.Parent will give you the humaniod, target gives you the Player in Players

0

if sc is the leaderstats then it should be sc.Parent = target and sc.Name needs to = "leaderstats" if your trying to use the game's default leaderstats.

0

@KenUSM Button2Down does not pass anything, don't give OP false information.

0

I've done everything I say but the error on line 15 -- userid is not a valid member of part

2 answers

✓ Accepted answer

###What you're doing, is trying to get the player from a BasePart. You'll have to get the player manually, using GetPlayerFromCharacter, a method of Players. The character is a Model.

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = Player:GetMouse()
local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(inp)
    if inp.UserInputType == Enum.UserInputType.MouseButton2 then
        local target = mouse.Target
        
        if target then
            local targetPlayer = Players:GetPlayerFromCharacter(target.Parent)
            
            if targetPlayer and targetPlayer ~= player then
                   sc.Player.Image = "https://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&userId="..targetPlayer.UserId --PlayerID
                    sc.Attack.Text = "Attack: "..targetPlayer.leaderstats.Attack.Value --leaderstats
                    sc.Agility.Text = "Agility: "..targetPlayer.leaderstats.Aggility.Value --leaderstats
                    sc.Defense.Text = "Defense: "..targetPlayer.leaderstats.Defense.Value --leaderstats
                    sc.Ki.Text = "Ki: "..targetPlayer.leaderstats.Energy.Value --leaderstats
                    sc.Username.Text = ""..targetPlayer.Name --PlayerName
            end
        end
    end

end)

###On a side note, switch to Connect, as connect is deprecated and should not be used in new work. Read more on deprecation here.

It's the same as how you accessed the LocalPlayer.

local tag = "xPolarium"

if game.Players[tag] then
	print("Found our desired player.")
end

In your case, you would use the name of hum to locate the player in Players.

Also use Connect over lower case connect as the latter is depricated and is likely to be removed in the future. Good practice to code right than risk anything.

3 comments

0

^

0

I'll try.

0

I tried this way: mouse.Button2Down: connect (function () local target = mouse.Target if game.Players [target] then --and did not work

This discussion is preserved as it was. New answers, comments, and votes are closed.