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

Press F to pickup? Combine LocalScript and Script?

Hello)

Could you help me solve this problem, I just do not understand.

So:

I want to create a script “Press F to pick up”, which (as I understand it) should contain these events:

Firstly, in the object should be a ClickDetector which intercepts the mouse hovering through the Script

script.Parent.ClickDetector.MouseHoverEnter: Connect (function (plr)
	print("HOVERED")
end)

Secondly, in the local script is intercepting the pressed button

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

function onKeyPress (inputObject, gameProcessedEvent)

	if inputObject.KeyCode == Enum.KeyCode.F then
		print("PRESSED")
	end
end

These events are in different scripts, how to intercept when they both work and then perform the necessary operations?

As I understand it, the capture of buttons on the keyboard should be a local script. To capture ClickDetector mouse hover should be a script.

Maybe there is some other way that works in one script?

THX :)

2 answers

✓ Accepted answer

Why not making it in a much simpler way?

Here is an idea:

  1. Create 1 local script inside of the player
  2. Create a Remote Event inside of Replicated Storage
  3. ModuleScript inside of ServiceScriptService
  4. Create a script inside of ServiceScriptService
  5. a string value inside of the part with the name of the function you want (example: wood)

local script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local get


function Hover(key)

 if inputObject.KeyCode == Enum.KeyCode.F and mouse.Target ~= nil then

if mouse.Target:FindFirstChild("STRINGNAME") then

local fun = mouse.Target.STRINGNAME.Value

game.ReplicatedStorage.REMOTEEVENTname:FireServer(fun)


end
end




end


mouse.KeyDown:Connect(Hover)

script:


game.ReplicatedStorage.OnServerEvent:Connect(function(player, fun)

local module = require(game.ServerScriptService.MODULESCRIPTname)

module.fun(player)


end)

module script:


local module = {}

function NameOfTheFunctionInTheValueInsideOfTheStringOfThePart(player)

--code here


end


return module

with this you will detecte whetever the object you have your mouse hover has a string value and if it does when you press F it will play the function in the module script (already server sided because of the remote even), if you have any question feel free to ask but please remember to upvote my answer! Have a great day :)

1 comment

0

Wow. It turned out to be easier than I thought. The only thing I replaced the Mouse.KeyDown function with UserInputService since I read that Roblox does not advise using it anymore because it is outdated. Thanks bro)

You could add a BoolValue or similar inside player or character, and when you do any of that functions, that Value goes to true. Then just check if the value is false and there. This is useful for dont stack things. Hope I helped.

1 comment

0

^I did read bad i think so ignore my answer

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