The player has many weapons. To kill an NPC, he needs to hold E for 2 seconds on a proximity prompt of the NPC.
I want to make it, so that different weapons change the time of the proximity prompt. So for example with an axe the hold duration will be 4 seconds, with dagger - 2 seconds.
The problem is, the game is multiplayer, so I’m not sure how to make what I want, but so that multiple players don’t interfere with each other when near same NPC.
Personally my idea would be to have it so when a player has an item equiped and out, like an axe, there would be a script (script1 for reference) that senses that the player pulled out that weapon of an axe.
Then it would communicate to another script in the proximity prompt, which it tells the script (script2) what weapon was drawed.
Then that script (script2) will take that variable of what weapon was drawed then pass it though an “if” statement.
The “if” statement is basically seeing what weapon was drawed then making an output depending on the weapon. So if the weapon was an axe, it would be if weapon == "axe" then proximityPromptName.HoldDuration = 4
and if it was a dagger, it would be if weapon == "dagger" then proximityPromptName.HoldDuration = 2
Make Sure You Scripts Are Running in a LocalScript.
The way this script is written is completely up to you. Im currently not able to launch studio due to write out some code, but if you look into the documentation you can possibly learn a few things from there.
A server-side approach could enable/disable prompts when in use, like in the following.
local Workspace = workspace
local Part = Workspace.Part
local Prompt = Part.ProximityPrompt
local HoldDurations = {
Tool1 = 2, --2 seconds with 'Tool1'.
Tool2 = 4 --4 seconds with 'Tool2'.
}
local function OnPromptButtonHoldBegan(Player)
local Character = Player.Character
if not Character then return end
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
if (not Humanoid) or Humanoid.Health <= 0 then return end
Prompt.Enabled = false --Hide the prompt.
local Tool = Character:FindFirstChildOfClass("Tool")
Prompt.HoldDuration = if Tool and HoldDurations[Tool.Name] then HoldDurations[Tool.Name] else 6 --6 seconds is the default hold duration.
end
local function OnPromptButtonHoldEnded(Player)
Prompt.Enabled = true --Display the prompt.
end
Prompt.PromptButtonHoldBegan:Connect(OnPromptButtonHoldBegan)
A client-side approach, which may be more in tune with what you’re trying to achieve would need to instance each prompt that is required (so that each client has their own set of prompts to interact with), you could then listen for the client’s character’s ‘ChildAdded’ signal to determine when a tool is equipped and act accordingly.
local Workspace = workspace
local Prompts = {}
local HoldDurations = {
Tool1 = 2,
Tool2 = 4
}
local function OnChildAdded(Tool)
if not (Tool:IsA("BackpackItem")) then return end
for _, Prompt in ipairs(Prompts) do
Prompt.HoldDuration = HoldDurations[Tool.Name] or 6
end
end
Character.ChildAdded:Connect(OnChildAdded)
for _, Prompt in ipairs(Workspace:GetDescendants()) do
if not (Prompt:IsA("ProximityPrompt")) then continue end
table.insert(Prompts, Prompt)
end
I definitely want to go by a server sided approach, but I have ran into issues.
prompt.Triggered:Connect(function(pl)
pl.Character.Humanoid:LoadAnimation(script.KillAnim):Play()
task.wait(0.5)
script.Parent.Parent.Parent.Humanoid.Health = 0
prompt:Destroy()
end)
prompt.PromptButtonHoldBegan:Connect(function(pl)
if pl.Character:FindFirstChild("Weapon") then
prompt.Enabled = false
prompt.HoldDuration = pl.Character.Weapon.Stats.KillTime.Value
script.KillAnim.AnimationId = pl.Character.Weapon.Stats.KillAnim.AnimationId
prompt.Enabled = true
end
end)
This works fine, and it actually takes different times to kill the NPC with different weapons, which I wanted. The problem is visual.
The default Hold Duration of proximity prompt is 5. Even tho it takes for example 10 seconds for the prompt to trigger, if the weapon killtime value is 10, the circle around the prompt still fills in 5 seconds. So I approach the enemy, hold the prompt, it fills in 5 seconds, then I wait 5 more seconds for the NPC to die.
Only if I approach him with a weapon, start holding the prompt but don’t hold fully, then go away so that prompt stops showing, and approach him again, only then the circle fill duration matches the new hold duration.