I’m trying to make a “repair generator” sorta mechanic, when you press down mb1 you ‘fix the generator’ and when you release it you stop.
How should i go about doing this? and also being able to send it to the server.
I’m trying to make a “repair generator” sorta mechanic, when you press down mb1 you ‘fix the generator’ and when you release it you stop.
How should i go about doing this? and also being able to send it to the server.
sending it to the server should be easy, just get a remote event and put it as a variable as you normally would, not really sure what arguments you’re going to put but doesn’t matter, also there might be an error in your question because localscripts don’t run when workspace is an ancestor, but continuing on.
for prompt behavior, there’s two connections you should know for holding it down and releasing:
.Triggered and .TriggerEnded, pretty self explanatory.
to connect these, it’s as simple as connecting an event.
local prompt = script.Parent -- or wherever it is
local event = your.event.here
prompt.Triggered:Connect(function()
print("triggered")
event:FireServer(yourthingstosendhere)
end)
prompt.TriggerEnded:Connect(function()
print("stopped")
end)
Roblox has a proximity prompt object for this exact scenario. If you didn’t create this post knowing about it, I strongly recommend you don’t try to reinvent the wheel and just use it like athony2025 says.
In the somewhat likely event that you did know about it, it’s a little hard to figure out which part about it you don’t like. I’ll just say how I would build it from ground up.
First step is to create the UI. I’d just use either an image or text button. How this looks like will be entirely up to you. The buttons’ MouseButton1Click event can be used to send a signal to the server side via a remote event to slowly run down a timer and the buttons’ MouseButton1Up event can be used to either reset the timer or stop running down the timer. If you want more functionality, like using keyboards and controller buttons, you’ll need to do some more work, though I doubt that’s the case, given you only mentioned pressing down a mouse button.
Next step is to get the UI to appear on top of parts you want. I’d use the WorldToScreenPoint camera method to figure out where on the screen the part is and to display the prompt window there after the player gets close enough to that part.
Edit: Because I enjoy coding, here’s a really rough proof of concept for you. There are a few really egregious problems with this, such as the distance system being completely unscalable and the lack of protections against respawning, just to name a few of the less obvious ones. However, I still think it’s good enough to mess around with the concepts that I presented in this post.
ProximityPromptPoc.rbxl (62.6 KB)
I was aware of proximityprompts and I will probably use those and combine it with UIS, but I don’t want them to have to click within a certain area, I just want them to be able to click (anywhere) and the system will trigger
You can emulate ProximityPrompt triggering through script using :InputHoldBegin() and :InputHoldEnd(), and you can broadly listen for nearby proximity prompts using ProximityPromptService’s PromptShown and PromptHidden events. This is a quick script that triggers all nearby active proximity prompts when you click, though you may want to modify what the script classifies as an appropriate prompt to watch for if there are only specific prompts you want this to apply to.
--// LocalScript
--[[ Variables ]]--
-- Services --
local ProximityPromptService = game:GetService("ProximityPromptService")
local UserInputService = game:GetService("UserInputService")
-- Prompt --
local activePrompts : {ProximityPrompt} = {}
--[[ Functions ]]--
local function handlePrompt(prompt : ProximityPrompt) : ()
prompt.PromptShown:Connect(function()
table.insert(activePrompts, prompt)
end)
prompt.PromptHidden:Connect(function()
table.remove(activePrompts, table.find(activePrompts, prompt))
end)
end
local function triggerActivePrompts()
for _, v in activePrompts do
v:InputHoldBegin()
v:InputHoldEnd()
end
end
local function onInputBegan(input : InputObject, gpe : boolean) : ()
if gpe or #activePrompts == 0 then
return
end
if input.UserInputType ~= Enum.UserInputType.MouseButton1 then
return
end
triggerActivePrompts()
end
local function onPromptShown(prompt : ProximityPrompt, inputType : Enum.ProximityPromptInputType) : ()
if not table.find(activePrompts, prompt) then
table.insert(activePrompts, prompt)
end
end
local function onPromptHidden(prompt : ProximityPrompt)
table.remove(activePrompts, table.find(activePrompts, prompt))
end
local function init() : ()
UserInputService.InputBegan:Connect(onInputBegan)
ProximityPromptService.PromptShown:Connect(onPromptShown)
ProximityPromptService.PromptHidden:Connect(onPromptHidden)
end
init()
this is exactly what i was looking for but could you explain what the use is for wrapping it in a function, just trying to learn something new, thanks!\
also sorry for the late reply
It’s just a habit of mine to avoid writing functions that do everything at once because it can be harder to pinpoint where something is going wrong, so I instead break it up as much as possible. It’s not necessary, but is better to work with in my opinion.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.