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
283 views
in Technique[技术] by (71.8m points)

Roblox: How to kill player if they don't complete a task correctly?

I'm using a pre-made code door in which the player(s) must enter correct passwords to pass through. I would like the last door the local player must go through to kill them if they guess the password wrong. The door is a 2323 code door. It already came with a script. I've tried to edit the code for the incorrect password:

Input = ""
local player = game.Players.LocalPlayer
local character = player.Character
character.health = 0
print("Wrong Code")

However, I get a nil value "Character." Any idea what I am doing wrong? Thanks.

question from:https://stackoverflow.com/questions/65876447/roblox-how-to-kill-player-if-they-dont-complete-a-task-correctly

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

1 Reply

0 votes
by (71.8m points)

You cannot access localplayer from a server script. Here is what you can do alternatively:

  1. Make server script
  2. Make a remote event
  3. Fire the remote event with the password as the arg
  4. Put this in your server script:
local remoteevent = -- reference remote event path here
local correctpass = 2323

remoteevent.OnServerEvent:Connect(function(p, pass)
    -- p is player and pass is the password the user entered
    if tonumber(pass) == correctpass then
        -- User entered correct pass so open the door
    else
        p.Character.Humanoid.Health = 0
    end
end)

And finally, fire the remote event from the local script just like this:

local remoteevent = -- reference remote event path here
local userinput = -- get the password the user entered here
remoteevent:FireServer(userinput)

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

...