My Studio is failing

What’s bad with this? Doesn’t works with local and non-local. :confused:

Players.OMQ_Kevin666.Backpack.InstantTeleport.LocalScript:2: attempt to index nil with 'GetMouse'
script.Parent.Activated:Connect(function(plr)
	local mouse = plr:GetMouse()

	plr.Character.PrimaryPart.CFrame = CFrame.new(mouse.Hit.p)
end)

That means GetMouse returned nil. Try:

script.Parent.Activated:Connect(function()
    local Player = game.Players.LocalPlayer
	local Mouse = Player:GetMouse()

	Player.Character.PrimaryPart.CFrame = CFrame.new(mouse.Hit.p)
end)

Thanks, it works, i guess this is from a useless Studio update no?

Nope! You were trying to get the player from the function, which works in some cases, but getting the player’s mouse requires you to check the player object, not a player reference.

2 Likes

https://developer.roblox.com/en-us/api-reference/event/Tool/Activated

The .Activated event when fired does not automatically pass the player object pertaining to the client which fired the event as an argument to any callback function connected to it, all you needed to do and what you should do is the following.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local tool = script.Parent

tool.Activated:Connect(function()
	hrp.CFrame = mouse.Hit
end)

This will also avoid redeclaring the same variables each time “Activated” fires, they only need to be declared once, as they are constants (variables with values which are not subject to change), hence their declarations can be moved outside of the callback function connected to the event.

Don’t forget that “mouse.Hit” is a CFrame value already so you can also just use that instead of constructing a new CFrame value by using the Vector3 value held by “mouse.Hit.p”.

1 Like