Welcome to OStack Knowledge Sharing 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...