Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get this handcuff scirpt to work?

Asked by 5 years ago
script.Parent.Handle.Touched:Connect(function(hit)
while(true) do
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
Players = game.Players:GetPlayers()
wait(1)
for i,v in pairs(Players) do
if v.Backpack:FindFirstChild("Gun") then
hit.Parent:MoveTo(game.Workspace.Prison.Position)
end
end
end
end)

I want the player to teleport (when touched by the handcuffs) to the prison if they have a gun in their backpack. But it's not doing anything when they are touched by the handcuffs!

1 answer

Log in to vote
0
Answered by
hudzell 238 Moderation Voter
5 years ago
Edited 5 years ago

It looks like you have a couple unnecessary loops in there, on line 2 and 8, as well as just a bunch of unnecessary lines. Try this instead

script.Parent.Handle.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player and player.Backpack:FindFirstChild("Gun") and hit.Parent:FindFirstChild("HumanoidRootPart") then --Fit all the checks into a single if
        hit.Parent.HumanoidRootPart.CFrame = workspace.Prison.CFrame + Vector3.new(0,3,0) --Moves the HumanoidRootPart, basically the centre/main part of the character, instead of the model, and offset the teleport 3 studs up so they don't get stuck in the ground
    end
end)
Ad

Answer this question