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

My touch event teleport every players when i dont want ?

Asked by 4 years ago

My problem is when a player touch the part it automaticly teleport every players at the 'Respawn' Part,

I just want to teleport the player who touch it

local amount = 250
debounce = true

script.Parent.Touched:Connect(function(hit)
    local player = game.Workspace[hit.Parent]
    print(player.Name)
    if player.Character then
        local stats = player:FindFirstChild("leaderstats")
        if stats then
            local gui = player.PlayerGui
            if gui then
                if debounce == true then
                    debounce = false                
                    stats.Cash.Value = stats.Cash.Value +amount
                    game.Workspace.TrainingMap.EndLavaMap.Sound:Play()
                    player.Character:MoveTo(workspace.Respawn.Position)
                    gui.PlayerFrame.Menu.AddRemove.Text = "+"
                    wait(1)
                    gui.PlayerFrame.Menu.AddRemove.Text = ""
                    debounce = true
                end
            end
        end
    end
end)

1 answer

Log in to vote
0
Answered by 4 years ago

A few problems with your script.

Line 5:

local player = game.Workspace[hit.Parent]

When looking for a player its best to do this:

local player = game.Player:FindFirstChild(hit.Parent.Name)

You are looking for the player's name when a player touches it so you can call for it in game.Players

Line 7:

if player.Character then

There is no character in a characters model so this should be removed. It would be best to check if a humanoid touched it to insert this line between line 4 and 5

if hit.Parent:FindFirstChild("Humanoid") then

Line 16:

player.Character:MoveTo(workspace.Respawn.Position)

Dont use :MoveTo() for teleporting players. Use CFrame

hit.Parent.HumanoidRootPart.CFrame = workspace.Respawn.CFrame

So your end result would look like this:

local amount = 250
debounce = true

script.Parent.Touched:Connect(function(hit)
  if hit.Parent:FindFirstChild("Humanoid") then
    local player = game.Player:FindFirstChild(hit.Parent.Name)
    print(player.Name)
        local stats = player:FindFirstChild("leaderstats")
        if stats then
            local gui = player.PlayerGui
            if gui then
                if debounce == true then
                    debounce = false                
                    stats.Cash.Value = stats.Cash.Value +amount
                    game.Workspace.TrainingMap.EndLavaMap.Sound:Play()
                    hit.Parent.HumanoidRootPart.CFrame = workspace.Respawn.CFrame
                    gui.PlayerFrame.Menu.AddRemove.Text = "+"
                    wait(1)
                    gui.PlayerFrame.Menu.AddRemove.Text = ""
                    debounce = true
                end
            end
        end
    end
end)

0
A) MoveTo() and CFrame do exactly the same thing B) it is actually better to get the player from their model in Workspace as opposed to within the Players service C) his character line can simply be 'if player then' since 'player' is the model DeceptiveCaster 3761 — 4y
Ad

Answer this question