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

[EDITED TITLE] Why isn't this script CFraming the player at all!?

Asked by 7 years ago
Edited 7 years ago

I'm trying to teleport the player character to a certain position using CFrame when a player touches an object.

My problem is that the player doesn't CFrame to a postion during online play but in Roblox Studio only, WHY?!

debounce = false
function Touch(Item)
    if debounce == true then return end
    debounce = true 
    if Item.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(Item.Parent)  then
        local Explosion = Instance.new("Explosion",script.Parent)
        local Torso = Item.Parent:FindFirstChild("Torso") 
        local Humanoid = Item.Parent:FindFirstChild("Humanoid")
        Explosion.BlastPressure = 500
        Explosion.BlastRadius = 30
        Explosion.DestroyJointRadiusPercent = 0
        Explosion.Visible = true
        Explosion.Position = script.Parent.Position
        Torso.Anchored = true
        local Step = game:GetService('RunService').Stepped:Connect(function() 
            Torso.CFrame = script.Parent.CFrame --THIS PART NO WORK!!!
            Humanoid.WalkSpeed = 5
        end)
        wait(0.5) 
        Step:disconnect()
        Torso.Anchored = false
        local Refract = Instance.new("BodyPosition",Torso)
        Refract.Position = script.Parent.Position + Vector3.new(0,30,0)
        Refract.MaxForce = Vector3.new(400000,400000,400000)
        Refract.D = 1000
        wait(0.5)
        debounce = false
        Refract:Destroy()
    end
end
script.Parent.Touched:Connect(Touch) 


0
use BodyPosition   iddash 45 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

The reason it's not working is that it's immediately disconnecting due to no wait being sent.

local connection = game:GetService("RunService").Stepped:Connect(function() 
--stuff
end) --This is in a seperate thread, due to it being an event.
wait(5) --solution!
connection:disconnect() --This seems not to be working because of the seperate threads!!!
Torso.Anchored = false
Ad
Log in to vote
0
Answered by
R_alatch 394 Moderation Voter
7 years ago
Edited 7 years ago
local Debounce = false

script.Parent.Touched:Connect(function(Hit)
    if Debounce then
        return
    end

    Debounce = true

    local BackupPart = script.Parent:Clone()
    local Player = game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent)

    if Player and Player.Character then
        local Humanoid = Player.Character:FindFirstChild("Humanoid")
        local Root = Player.Character:FindFirstChild("HumanoidRootPart")

        if Humanoid and Humanoid.Health > 0 and Root then
            local Explosion = Instance.new("Explosion", workspace)
            Explosion.DestroyJointRadiusPercent = 0
            Explosion.Position = script.Parent.Position

            Root.CFrame = CFrame.new(script.Parent.Position)
            Root.Anchored = true

            script.Parent:Destroy()

            wait(2)

            Root.Anchored = false
        end
    end

    Debounce = false

    wait(2)

    BackupPart.Parent = workspace
end)

Answer this question