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

any help with this "teleport to place if have badge" script not working?

I'm trying to make a script where once the player presses K on their keyboard, the script checks if they have a badge. If they do, they teleport to a specific place. If they dont, it does nothing except print out a message, but for some reason its not working. Any help? I might be doing something wrong here

local players = game.Players.LocalPlayer
local BadgeService = game:GetService("BadgeService")
local BadgeId = 2124514373
local mouse = players:GetMouse()


mouse.KeyDown:Connect(function(Key) 
 if Key == "k" then
	game:GetService("Players").PlayerAdded:Connect(function(player)
    if BadgeService:UserHasBadge(player.UserId, BadgeId) then
		local PlaceId = 4767501208
		game:GetService("TeleportService"):Teleport(PlaceId, players.Character)
		print("I hopefully worked")
	else
		wait(0.1)
		print("I am a silly goof and don't have the badge")
	end
	 end)
end
end)

1 answer

#Problems

  1. Mouse is Superseded by UserInputService and ContextActionService

  2. Why did u Connect to PlayerAdded

    • So u basically said When the Player clicks K then Wait for Some other new player

#Solutions

  1. Instead of using Mouse use ContextActionService:BindAction()
  2. Don't have Connect to a Event and Check if a New player joins the Game.
    • How did u even Came up with that??
    • And you never even Used the players varable.
      • And why is it Called players it should be player since it points to the Local Player

#Final Script Make sure this is a LocalScript

local player = game.Players.LocalPlayer -- Change variable name to make it Less Confusing
local BadgeService = game:GetService("BadgeService")
local BadgeId = 2124514373
local PlaceId = 4767501208 -- I like to put Constants at the top

local CAS = game:GetService("ContextActionService") -- Use ContextAction Server instead of Mouse

local function OnKPressed(Action, State, Input) -- Called when Player presses K
	if State == Enum.UserInputState.Begin then -- Check if the Player started to Press K (Equalivent to Mouse.KeyDown)
		
		if BadgeService:UserHasBadge(player.UserId, BadgeId) then
			game:GetService("TeleportService"):Teleport(PlaceId) -- Player argument is Omitted because it defaults to LocalPlayer
			print("I hopefully worked")
		else
			-- Why did u say wait(0.1) ?
			print("I am a silly goof and don't have the badge")
		end
	end
end

CAS:BindAction("Teleport", OnKPressed, false, Enum.KeyCode.K) -- Bind action to Function

You don't need to keep the comments ( Everything Green ) Its just to explain what i'm doing

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