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
script.Parent.Touced:Connect(function()
so i'm using a regular script, but i don't know how to address the player without using
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:
function getPlayerFromCharacter(descendant) for _, Player in pairs(game:GetService("Players"):GetPlayers()) do -- Iterates through a list of all Players if descendant:IsDescendantOf(Player.Character) then -- Checks if the Player the for loop is currently at's Character has Hit as a descendant return Player -- Returns the Player end end end script.Parent.Touched:Connect(function(Hit) local Player = getPlayerFromCharacter(Hit) if Player then --Insert your code here end end)
Don't forget to mark my answer as the solution and upvote it if it answered your question :)
Here you go
script.Parent.Touched:Connect(function(plr) -- the plr between the parameters is a part that came in contact with the part. local player = game:GetService("Players"):GetPlayerFromCharacter(plr.Parent) if player then --enter script here end end)
Hope this helps
<3