Ad
Log in to vote
0

How to fetch a players name using a script?

Asked by 14 days ago

I realised I did not explain it well in the last post but what I am looking for is the brickblast not spawn when a players joins or when the character spawns. What I am looking for is the model to be in server storage and in the model will have a script. When the model gets added to workspace the script inside the model will run. I need a way to fetch the players name without using PlayerAdded or CharacterAdded (also this is a single player game). What I want is that there's a button in workspace and the player clicks it, it runs a while loop and the while loop will be like.

While game.workspace.attackchanger.Value == true do

game.serverstorage.Projectiles.BrickBlast:Clone.Parent = game.workspace

Wait(0.5)

end

Each time this while loop runs and the brick blast spawns the script inside the model will run right when the model gets placed in workspace. The model will get the players name and puts the models position on the players humanoidRootPart.

More context The brick blast is an attack so I need multiple to spawn. I hope I can get this fixed ^_^

0
just do game:GetService("PlayersService").LocalPlayer.Name MattVSNNL 378 — 13d

2 answers

Log in to vote
0
Answered by 14 days ago

im not sure i quite understand but if it's in a local script you can do game:GetService("Players").LocalPlayer.Name

or if you want the player's display name you can do

game:GetService("Players").LocalPlayer.DisplayName

Ad
Log in to vote
0
Answered by 14 days ago

If its a one player server, it should be a lot easier to get the player. You can get the players name by doing this.

game.Players:FindFirstChildOfClass('Player').Name

you can detect when the model "BrickBlast" moves into the workspace, put this inside the model

script.Parent.Changed:Connect(function(property)
    if property == 'Parent' then
        if script.Parent.Parent == game.Workspace then
            local player = game.Players:FindFirstChildOfClass('Player')
            local playername = player.Name
            -- do run script here

            -- this will move it to the players humanoidrootpart
            assert(player.Character, 'player without character')
            script.Parent:SetPrimaryPartCFrame(player.Character.PrimaryPart.CFrame)
        end
    end
end)

Answer this question