I'm scripting a morpher so that when someone steps on it they morph into that character. I think it needs to be a regular script to do
1 | script.Parent.Touced:Connect( function () |
so i'm using a regular script, but i don't know how to address the player without using
1 | local plr = game.Players.LocalPlayer |
because that only works with local scripts.
Why it's better to do it this way:
Grainless_Bread's answer will work in most cases but this will work as long as Hit
is a descendant
of the Player
whereas his only works if the Character
is the immediate Parent of Hit
The Solution:
01 | function getPlayerFromCharacter(descendant) |
02 | for _, Player in pairs (game:GetService( "Players" ):GetPlayers()) do -- Iterates through a list of all Players |
03 | if descendant:IsDescendantOf(Player.Character) then -- Checks if the Player the for loop is currently at's Character has Hit as a descendant |
04 | return Player -- Returns the Player |
05 | end |
06 | end |
07 | end |
08 |
09 | script.Parent.Touched:Connect( function (Hit) |
10 | local Player = getPlayerFromCharacter(Hit) |
11 | if Player then |
12 | --Insert your code here |
13 | end |
14 | end ) |
Don't forget to mark my answer as the solution and upvote it if it answered your question :)
Here you go
1 | script.Parent.Touched:Connect( function (plr) -- the plr between the parameters is a part that came in contact with the part. |
2 | local player = game:GetService( "Players" ):GetPlayerFromCharacter(plr.Parent) |
3 |
4 | if player then |
5 | --enter script here |
6 |
7 | end |
8 | end ) |
Hope this helps
<3