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

Why isn't the ball coming to the player's position?

local m = game.Players.LocalPlayer:GetMouse()


m.KeyDown:Connect(function(key)
	if key == "x" then
		print("hi")
		game.Workspace.Hamster.Position = game.Players.LocalPlayer.Character.Position
		end
	end)

so what it is supposed to do is teleport a the Union(Hamster) to the players location when x is pressed

Hi is not printed when I press x but if I remove

game.Workspace.Hamster.Position = game.Players.LocalPlayer.Character.Position

hi will be printed

1 comment

0

Why are you getting the mouse to check for a key? it is deprecated

1 answer

✓ Accepted answer

Well, for start, DONT USE KEYDOWN. It's deprecated. Use UserInputService, now with the main problem, it's not coming because character is a model, and not a part, that means it doesnt got "position" itself. Instead of that script, Use this:

local Hamster = workspace.Hamster
local player = game.Players.LocalPlayer
local char = player.Character
game:GetService("UserInputService").InputBegan:Connect(function(input, gameEvent)
	if input.KeyCode == Enum.KeyCode.X and not gameEvent then
		print("X fires.")
		Hamster.Position = char.HumanoidRootPart.Position -- I assume "Hamster" is a part, if its another model it wont work.
	end
end)

Here are some notes:

-As you did this in a local script, the hamster will be only visible for you, and not for the people around, if you want other people to see it, you have to fire a remote.

-Please, Never use deprecated things such as KeyDown. UserInputService is quite better.

-The second parameter of UserInputService "gameEvent" is if the player is writing on the chat and it press they key, so that means, if the player press they key and is not on the chat the function will fire.

I hope I helped, any question comment me (also it would be nice if u upvote) now, bye!

3 comments

0

DONT USE INPUT BEGAN ON SOMETHING THAT ONLY NEEDS A CLICK

0

Thanks, it worked, but in what ways is UserInputService better?

0

For start, its not deprecated. Userinput service, as I said with the notes, you can control the button if the player is chatting, and also there are many functions, as InputBegan (when u pressed the key) or InputEnded (when you left it)

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