Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Local script not recognizing when a player pressed a button?

Asked by 6 years ago

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

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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.

Ad
Log in to vote
0
Answered by 6 years ago

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

Log in to vote
0
Answered by 6 years ago

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

Answer this question