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

How do I access the player through the character?

Asked by 4 years ago
local player = hit.Parent:GetPlayerFromCharacter()  

I've made a teleport script that teleports players into the fighting arena for my game. Just recently I decided to look back into the script to add in the "inCombat" mode once they've used the teleporter, but in order to turn on the value that's in the player's stats folder I have to get to the player.

The teleporter is a simple touch event. I thought if I were to get the player who is touching the part, and reference their character by doing hit.Parent then adding :GetPlayerFromCharacter() it would lead me to be able to enter the stats folder in the player.

I'm assuming I'm using :GetPlayerFromCharacter() wrong, so is there a way I can access the player through a .touched event at all?

16:11:08.543 - GetPlayerFromCharacter is not a valid member of Model 16:11:08.544 - Stack Begin 16:11:08.544 - Script 'Workspace.DoorTeleporter.Teleport', Line 20 16:11:08.544 - Stack End

is the error that I'm receiving.

I tried checking if maybe I'm not getting the actual character model in the workspace by using print(). I put in print(hit.Parent) and it gave me the character name, so I'm completely lost on why it's not working.

Does :GetPlayerFromCharacter() have a different function that I'm not understanding? And if it does, what's the proper way to access the player through a .touched event? Is that possible?

2 answers

Log in to vote
0
Answered by
pwx 1581 Moderation Voter
4 years ago

You are close.

You would use the Player service like you are using, but you put the Character in the parameter.

local Players = game:GetService('Players')

local Part = workspace.Part

Part.Touched:Connect(function(otherPart)
    local Player = Players:GetPlayerFromCharacter(otherPart.Parent)
    if Player then
        print(Player.Name)
    end
end)
0
I literally just solved this issue by checking in the ROBLOX Developer page and looking into :GetPlayerFromCharacter() and it told me the same thing. But thank you so much for the help; always appreciate the answers. Next time I'll definitely also look at the ROBLOX Developer page before asking questions. Would've saved lots of time. EstrangedFisherman 51 — 4y
Ad
Log in to vote
0
Answered by
Derzett 51
4 years ago

Don't worry! Just a little mistake you made! It should be:

game.Players:GetPlayerFromCharacter(hit.Parent)

Actually i made the same mistake in the past but quickly fixed it. Good luck on your game though :)

Answer this question