New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How would i get the player from the character?

Asked by 4 years ago

[All I want to know is what would I put in the parameters for the :GetPlayerFromCharacter(), or what could I do to get the player]

Code(it is in a TextButton that is inside a Gui that is inside StarterGui):

function onClicked()
local p = game.Players:GetPlayerFromCharacter()
local t = script.Parent.Parent.Parent.Parent.Character.Torso
if p.TeamColor == "Bright bluish green" then
t.CFrame = game.Workspace.MiningTycoon.Tycoons.Diamond.Essentials.Spawn.CFrame
elseif p.TeamColor == "Really red" then
t.CFrame = game.Workspace.MiningTycoon.Tycoons.Ruby.Essentials.Spawn.CFrame

end 
end 

script.Parent.MouseButton1Down:connect(onClicked)

2 answers

Log in to vote
1
Answered by
xuefei123 209 Moderation Voter
4 years ago

Well, you don't need to use :GetPlayerFromCharacter(), because this is in the player all ready, you could just do:

local player = script.Parent.Parent.Parent.Parent--This would work if the TextButton's parent was a screen GUi in the StarterGui

But to use the GetPlayerFromCharacter, you still need the player, eg

local player = script.Parent.Parent.Parent.Parent
function onClicked()
local p = game.Players:GetPlayerFromCharacter(player.Character)
local t = script.Parent.Parent.Parent.Parent.Character.Torso
if p.TeamColor == "Bright bluish green" then
t.CFrame = game.Workspace.MiningTycoon.Tycoons.Diamond.Essentials.Spawn.CFrame
elseif p.TeamColor == "Really red" then
t.CFrame = game.Workspace.MiningTycoon.Tycoons.Ruby.Essentials.Spawn.CFrame

end 
end 

script.Parent.MouseButton1Down:connect(onClicked)

If this helps, why don't you click that accept answer button? It gives us both rep!!

0
Please can you accept this answer?? xuefei123 209 — 4y
0
Is this what Scripting Helpers has become? A place that used to have good questions, a place that people only answer questions for reputation instead of for the sake of helping people's games? You can't only like the reputation, which is a title, and not want to do it for the other people. You asking for acceptance twice? LordDragonZord 3358 — 4y
0
Sorry if it seems mean but, It's the truth, gotta do things the hard way. I lost 100+ reputation because of a guy named Jaja. LordDragonZord 3358 — 4y
Ad
Log in to vote
-1
Answered by 4 years ago

To use :GetPlayerFromCharacter(), the parameter is for the Character. Now, with a GUI you wouldn't really have a good reason to use it, at least, not with what you're doing. You should always use a localscript for GUI's which to get the player for a LocalScript, all you need to do is local Variable = game.Players.LocalPlayer and now you have the Player.

Now, I will show you a quick script that will print the Players name when a player touches it, getting the player with GetPlayerFromCharacter() as an example for later use.

script.Parent.Touched:connect(function(hit) -- assuming script is in the part
    local Char = hit.Parent -- the model containing the players part is considered the Player, AKA Figure.
    local Plr = game.Players:GetPlayerFromCharacter(Char)
    print(Plr.Name.." has touched this part")
end)

Answer this question