Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

3 comments

0

Do you mean it teleports the people who dont have the gamepass? because i dont get your description.

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.

0

Don't ask for us to do your work, we aren't slaves.

1 answer

✓ Accepted answer

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)

1 comment

0

Where would I put this in the script?

This discussion is preserved as it was. New answers, comments, and votes are closed.