#How to find player name when they join,

81 messages · Page 1 of 1 (latest)

rocky anchor
#
game.Players.PlayerAdded:Connect(function(players)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = players
    
    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = leaderstats
    Coins.Value = 0
    

    
end)

local player = game.Players
local name = player:GetPlayerFromCharacter(player)
print(name)

    
game.UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        game.Players.name.Coins.Value += 1 
    end

    if input.KeyCode == Enum.KeyCode.E then
        print("Pressed E")
    end
end)
#

I can find it using otherPart.Parent when they stand on a block but I dont want that

#

basically, code is when they reset, +1 deaths

knotty wigeon
#

stop skidding

compact dawn
final snow
tawny idol
#

brb setting up autoclicker to give myself infinite coins

leaden blade
steep nacelleBOT
#

studio** You are now Level 12! **studio

leaden blade
#

So like

rocky anchor
#

I am just doing random practise codes

rocky anchor
# leaden blade PlayerAdded returns the player who joned so when you connect it to a function yo...

I mean this aint working tho

local Player = game.Players.PlayerAdded:Connect(function(Player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player
    
    local Username = Instance.new("StringValue")
    Username.Name =  "Username"
    Username.Parent = leaderstats
    Username.Value = Player.Name
    
    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = leaderstats
    Coins.Value = 0
    
    
    
end)

local CoinBag = game.Workspace.Door


CoinBag.Touched:Connect(function(otherPart)
    local character = otherPart.Parent 
    local humanoid = character:FindFirstChildOfClass("Humanoid")


    local hit = true
    print(hit)



    if humanoid and hit == true then
        print("Hello???")
        print(Player.Name)
        Player.Name.leaderstats.Coins.Value = Player.Name.leaderstats.Coins.Value +1
        hit = false    
        print(hit)

        task.wait(1)
    end


end)
tawny idol
rocky anchor
#

debounce

tawny idol
#

if you're trying to do a debounce it won't work because every time it's touched it's resetting

rocky anchor
#

oh

tawny idol
#

local deb = true if deb then is basically just adding useless words

tawny idol
rocky anchor
#

?

tawny idol
#

what you've concepted to do is change the player we're talking about every time someone joins the game

#

so whenever anyone touches a coin it would give money to the last guy to join the game

rocky anchor
#

Oh

tawny idol
#

and also the (Player) in brackets is the one used inside that function so player = blah is like not what u wanna be doing here

#

just do local plr = game.players:getplayerfromcharacter(character) if plr then

#

cuz u can't access the player inside the touched function

tawny idol
#

they're two separate blocks of code that don't communicate

rocky anchor
tawny idol
#

how will what do that

rocky anchor
#

the code your suggesting

#

has nothing to do with the part

tawny idol
#

that's how you get which player stepped on the part

#

use the get player from character functionality of the playersservice

rocky anchor
# tawny idol that's how you get which player stepped on the part

Is there a way to make this available to the whole script?


game.Players.PlayerAdded:Connect(function(players)
    
    
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = players
    
    local Username = Instance.new("StringValue")
    Username.Name =  "Username"
    Username.Parent = leaderstats
    Username.Value = players.Name
    
    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = leaderstats
    Coins.Value = 0
    --leaderstats
    
end)

#

thats why I was doing local player = this

tawny idol
#

you shouldn't in this situation

#

you're correct in that giving players their leader stats and giving them money when they touch a part are two separate concepts

#

all you need to do is run the code i gave you

rocky anchor
# tawny idol you shouldn't in this situation

game.Players.PlayerAdded:Connect(function(players)
    
    
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = players
    
    local Username = Instance.new("StringValue")
    Username.Name =  "Username"
    Username.Parent = leaderstats
    Username.Value = players.Name
    
    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = leaderstats
    Coins.Value = 0
    --leaderstats
    
    --locate player
    local PlayerName = players.Name
    print(PlayerName)
    --got player


    local CoinBag = game.Workspace.Door



    CoinBag.Touched:Connect(function(otherPart)
        
        local character = otherPart.Parent
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        --if a character touches it
        
        local PlayerName = players
        
        if humanoid then
            local hit = true
            
            if humanoid and PlayerName and hit == true then
                while hit == true do
                    PlayerName.leaderstats.Coins.Value += 1
                    task.wait(1)
                    break
                end
                
                hit = false

            end
        end
        
        



    end)
    
end)

AND IT WORKS

#

JUST A FEW ATTEMPS

#

holy i am happy

tawny idol
#

don't do that

#

now every time someone touches a coin it will give everyone in the game money

#

you also set player name twice in the same function and like rather uselessly

#

also while hit == true do blah break is insane work

rocky anchor
# tawny idol also while hit == true do blah break is insane work

I added the debounce this time

local debounce = false
game.Players.PlayerAdded:Connect(function(players)
    
    
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = players
    
    local Username = Instance.new("StringValue")
    Username.Name =  "Username"
    Username.Parent = leaderstats
    Username.Value = players.Name
    
    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = leaderstats
    Coins.Value = 0
    --leaderstats
    
    --locate player
    local PlayerName = players.Name
    print(PlayerName)
    --got player


    local CoinBag = game.Workspace.Door



    CoinBag.Touched:Connect(function(otherPart)
        
        local character = otherPart.Parent
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        --if a character touches it
        
        local PlayerName = players
        
        if humanoid and not debounce then
            debounce = true
            
            if humanoid and PlayerName and debounce == true then
                while debounce == true do
                    PlayerName.leaderstats.Coins.Value += 1
                    break
                end
                
                

            end
        end
        
        wait(1)
        
        debounce = false 
        
        



    end)
    
end)


rocky anchor
#

I gues it dont matter

#

tbnh

#

tbh

#

since its in the if

tawny idol
#

try touching the coin with player1

rocky anchor
#

Fair enough

tawny idol
#
game.Players.PlayerAdded:Connect(function(players)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = players
  
    local Username = Instance.new("StringValue")
    Username.Name =  "Username"
    Username.Parent = leaderstats
    Username.Value = players.Name
    
    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Parent = leaderstats
    Coins.Value = 0
end)

local debounce = false
local CoinBag = workspace.Door
CoinBag.Touched:Connect(function(otherPart)
  if not otherPart.Parent then return end --guy died
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    --if a character touches it
    local PlayerName == game.Players:GetPlayerFromCharacter(character) --find player
    if humanoid and PlayerName and debounce == false then
        debounce = true
        PlayerName.leaderstats.Coins.Value += 1
        task.wait(1)
        debounce = false 
    end
  end
end)
#

can you see the difference here

rocky anchor
#

If not otherPart.Parent

rocky anchor
tawny idol
#

there's more than that that one's kinda inconsequential

#

just catching if the part like gets deleted at the same time so we don't error looking for its parent

#

i'm talking more philosophically

#

the biggest is using that get player from character thing i was talking about

#

that's how we access the player who touched

#

but i also cut out all the wack crap going on with the debounce

#

saying debounce = true if debounce == true is frankly a little silly because it's always going to be true

#

and the concept of checking if debounce is false, setting it to true and then checking whether it's true is insane because we already know the value of debounce and we then set it to something else so we know it twice and then try to figure out what it is a third time

#

and adding a while loop for no reason is also nuts

#

i think u might've over engineered it a bit cuz stuff wasn't working

#

but try to remember the fundamentals and maybe take a walk around outside if stuff isn't working and come back to it

rocky anchor
steep nacelleBOT
#

studio** You are now Level 8! **studio

tawny idol
#

that's fine that's why we're learning

#

try to break problems down into individual parts and find stuff on the internet about it

#

like how to get player who touched part etc

rocky anchor
tawny idol
#

nice