So, i want to make it so that once you spawn you will be automatically given the first tool in the Player Backpack, but you can not deselect/unequip the tool and you can only switch to the other tools by pressing one of the number keys on the keyboard.
task.wait(1)
local tool = script.Parent
local player = game.Players.LocalPlayer
local hum = player.Character.Humanoid
tool.Equipped:Connect(function()
game:GetService("RunService").RenderStepped:connect(function()
if not hum.Parent:FindFirstChild(tool.Name) then
hum:EquipTool(tool)
end
end)
end)
I am asking this because i want this feature to be used in my horror and FPS games.
This is my first devforum post. Also, i apologize if i am asking too much.
Put that in a local script in StarterCharacterScripts
To be able to switch to other tools I would have a local script using ContextActionService, placed in StarterCharacterScripts
This is pseudo-code and not tested but you might get it to work to your own liking and improve upon it:
--Services
local contextAction = game:GetService("ContextActionService")
local starterGui = game:GetService("StarterGui")
local playerService = game:GetService("Players")
--Disable Tool CoreGui
starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
--Player Stuff
local player = playerService.LocalPlayer
local backpack = player.Backpack
--Main Switch Tool
local function switchTool(action, inputState)
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
--Switch to first
if action == "switchFirst" and Enum.UserInputState.Begin then
local tool = backpack:FindFirstChild("") --Insert Tool Name
if tool then
humanoid:UnequipTools()
tool.Parent = character
end
end
--Switch to second
if action == "switchSecond" and Enum.UserInputState.Begin then
local tool = backpack:FindFirstChild("") --Insert Tool Name
if tool then
humanoid:UnequipTools()
tool.Parent = character
end
end
end
contextAction:BindAction("switchFirst", switchTool, true, Enum.KeyCode.One)
contextAction:BindAction("switchSecond", switchTool, true, Enum.KeyCode.Two)