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

Local script not recognizing when a player pressed a button?

I have a local script inside a campfire and when a player is near the campfire and they press "F" its supposed to print because i am just testing it, but it doesnt do anything. The script doesnt even run the print at the top. So I think the script isnt running at all.

Heres the code

local Player = game.Players.LocalPlayer

print("Active")

function OnKeyPress(InputObject, GameProcessedEvent)
	if not GameProcessedEvent then
		local Magnitude = (Player.Character.HumanoidRootPart.Position - script.Parent.HitBox.Position).magnitude
		if InputObject.KeyCode == Enum.KeyCode.F and Magnitude >= 0 and Magnitude <= 7.5 then
			print("F Pressed")
		end
	end	
end

game:GetService("UserInputService").InputBegan:Connect(OnKeyPress)

I am convinced the script isnt even running, any ideas?

I dont wanna switch it to a normal script, that will cause far to much extra work.

3 answers

✓ Accepted answer

As outlined in the API Docs, LocalScripts only work in the following:

A Player's Backpack, such as a child of a Tool, A Player's Character model, A Player's PlayerGui, A Player's PlayerScripts, The ReplicatedFirst service

(http://wiki.roblox.com/index.php?title=API:Class/LocalScript)

I would recommend using Remote Events/Functions to communicate between the server scripts in the campfire and the LocalScripts in StarterPlayerScripts.

If the very top isn't running check if the script is disabled in its properties. since theres no reason for the top print not to work

The reason the top doesn't print is because you are using a LocalScript in the workspace. LocalScripts can only be run local (on the client's computer) not on the server. Put the LocalScript in StarterGui and change

script.Parent.HitBox

to wherever it is in the workspace. for example

game.Workspace.CampFire.HitBox

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