Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

lua - How do I go about making a change character ontouch script?

so I would like to make a script that basically calls a function whenever a block is touched, and when the block is touched, change the character of whoever touched the block only to another character model. Can anyone help?

local circle = script.Parent
local plr = game.Players.LocalPlayer
circle.Touched:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
    plr.Character = "O" 
end
end)
question from:https://stackoverflow.com/questions/65945025/how-do-i-go-about-making-a-change-character-ontouch-script

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Local scripts don't run when they are a child of a part. They also won't run when there's a touch event(in this situation). It's much easier to use a script. The Character property contains a reference to a Model containing a Humanoid, body parts, scripts, and other objects required for simulating the player’s avatar in-game. The model is parented to the Workspace, but may be moved. Here is a sample code:

local PlayersService = game:GetService('Players')

local function Ontouch(part)
    if part.Parent:FindFirstChild("Humanoid") then
       local playerModelName = part.Parent.Name -- gets the name of the player model
       local player = PlayersService:FindFirstChild(playerModelName) 
       player.Character = workspace.Dummy -- write the path to the character you want
   end
end

script.Parent.Touched:Connect(Ontouch)

Make this a Script(not a local one) and make it the child of the part you want to touch and get the character. For the camera to follow the newly assigned character, you need to write a script to do that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...