So basically I want to create a zombie survival game, I have created my gun model and also scripted it, but the whole hold e without the Proximity Prompt stressed me out, I was literally searching for tutorials for hours but could not find one. If anyone could give me a tutorial on it, that would be a lot of help. I tried the Proximity Prompt but it just didn’t work, no errors popped up or anything.
How are you coding the prompt, and what is it for? I hope you mean holding E to get the gun, if so, you’ll need to store the gun(s) inside its own folder in ServerStorage for the server to pull the gun from. What you’ll also need is to get the information of the gun that the prompt is for. At that point, it should be straight-forward with what you’ll have to do next.
Here’s just a quick mockup of what I’m talking about:
-- Script In ProximityPrompt
local ServerStorage = game:GetService("ServerStorage")
local Guns = ServerStorage:WaitForChild("Guns") -- or where ever the folder is
local ProximityPrompt = script.Parent
local GunName
-- there are different ways of storing the information of what type of gun it is
-- we could use a StringValue that's stored within the proximity or gun model:
-- GunName = ProximityPrompt.GunName.Value --(where GunName is a StringValue)
-- as an attribute:
-- GunName = ProximityPrompt:GetAttribute("GunName")
-- or just as a string within the script, which we will use
GunName = "M9"
ProximityPrompt.Triggered:Connect(function(player)
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
local GunTool = Guns:FindFirstChild(GunName)
-- normally, we would want to check if the player already has a gun.
-- here's my way of doing that:
local HasGun = humanoid:GetAttribute("HasGunEquipped")
if GunTool and not HasGun then
humanoid:SetAttribute("HasGunEquipped", true)
GunTool:Clone().Parent = player.Backpack
-- we could also equip the player with the gun this way:
humanoid:EquipTool(GunTool:Clone())
end
end)
-- Happy coding!
-- edozune 1/31/2023
:Clone()??
You mean this?
humanoid:SetAttribute("HasGunEquipped", true)
local __TOOL = GunTool:Clone()
__TOOL.Parent = player.Backpack
humanoid:EquipTool(__TOOL)
Also to make things easier just make a string attribute to the part(or prompt) then do this.
local GunName:string? = script.Parent:GetAttribute("Gun")
`
You could also use collectionservice to loop through the prompts then use a function for them rather than reusing the same script for each prompt.
Here is a snippet that uses collectionservice and makes the game more user-friendly and easy to add guns.
--Made variables more readable
local CollectionService = game:GetService("CollectionService")
local ServerStorage = game:GetService("ServerStorage")
local Equip: boolean? = false -- Setting
local Connections = {}
function CheckIfPlayerOwnsGun(Player:Player?, __TOOL:Tool?)
if Player.Character:FindFirstChild(__TOOL) then return true end
if Player.Backpack:FindFirstChild(__TOOL) then return true end
end
function CloneGun(ProximityPrompt: ProximityPrompt?)
if not ProximityPrompt:IsA("ProximityPrompt") then return end
Connections[ProximityPrompt] = {}
table.insert(Connections[ProximityPrompt], ProximityPrompt.TriggerEnded:Connect(function(Player)
if Player.Character:FindFirstChildOfClass("Humanoid").Health > 0 then -- Preventing any bugs
local GunName:string? = ProximityPrompt:GetAttribute("GunName")
local GunItSelf:Tool? = ServerStorage:FindFirstChild(GunName)
if not CheckIfPlayerOwnsGun(Player,GunItSelf) then
local ClonedGun = GunItSelf:Clone()
ClonedGun.Parent = Player.Backpack
if Equip then
Player.Character:FindFirstChildOfClass("Humanoid"):EquipTool(ClonedGun)
end
else
warn("YOU ALREADY HAVE THIS GUN")
end
end
end))
end
do
local Guns = CollectionService:GetTagged("GunProximity")
for Objects=1, #Guns do
local Proximity = Guns[Objects]
CloneGun(Proximity)
end
end