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

Can anyone fix this script so that it doesn't teleport everyone? [closed]

Asked by
Fewice 22
5 years ago

So i've got this script that is supposed to teleport anyone who owns this gamepass to a certain location but whenever anyone steps on it, it teleports the people with the gamepass. If anyone can rewrite this to get it working that would be appreciated!

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamePassID = 2003331  -- Change this to your game pass ID 

function onPlayerSpawned(player) 

    local hasPass = false

    -- Check if the player already owns the game pass
    local success, message = pcall(function()
        hasPass = MarketplaceService:UserOwnsGamePassAsync(player.userId, gamePassID)
        print("Has Game Pass")
    end)

    -- If there's an error, issue a warning and exit the function
    if not success then
        warn("Error while checking if player has pass: " .. tostring(message))
        return
    end

    if hasPass == true then
        script.Parent.Touched:Connect(function(Hit)
    game.Workspace:WaitForChild(player.Name).HumanoidRootPart.CFrame = CFrame.new(212.15, 8.533, -130.643)
        end)
    end
end 

game.Players.PlayerAdded:connect(function(player)
  player.CharacterAdded:connect(function()
    onPlayerSpawned(player)
  end)
end)

-- Connect 'PlayerAdded' events to the 'onPlayerAdded()' function
Players.PlayerAdded:Connect(onPlayerSpawned)
0
Do you mean it teleports the people who dont have the gamepass? because i dont get your description. ieatandisbaconhair 77 — 5y
0
Let's say there is me in the game(who owns gamepass) and a noob who doesn't own it. If the noob steps on the teleport it won't affect him but it will teleport me to the other place. Fewice 22 — 5y
0
Don't ask for us to do your work, we aren't slaves. User#19524 175 — 5y

Closed as Not Constructive by User#19524

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
0
Answered by 5 years ago

Put your touched event outside of the function, and check like this:

part.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum and hum.Health > 0 then
        local plr = game:GetService("Players"):GetPlayerFromCharacter(hum.Parent)
        --check if owns gamepass here, you have a plr variable and now check if owns gamepass, if yes then teleport, if not ignore
    end
end)
0
Where would I put this in the script? Fewice 22 — 5y
Ad