Hello Devforum! Recently, I have been developing a raycasting gun with a bullet and reload system. I have the gun down, but now one of the only issues is the player can still shoot and reload even though the gun isn’t even equipped. I am very confused on why this happens.
Here is an example video:
As you can see, I can shoot and reload even though the gun isn’t equipped. I have been checking my code trying to add some values to indicate whether the gun could shoot or reload, but it didn’t work. I just went back to my original code.
Here is the code I have:
Local script:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local gunhold = character:WaitForChild("Humanoid"):LoadAnimation(game.ReplicatedStorage.GunAnimations.Pistol.Hold)
local reload = character:WaitForChild("Humanoid"):LoadAnimation(game.ReplicatedStorage.GunAnimations.Pistol.Reload)
local mouse = player:GetMouse()
local bulletsleft = script.Parent.Bullets -- value in gun about the amount of bullets if you are confused
local inputservice = game:GetService("UserInputService")
local reloading = false
script.Parent.Equipped:Connect(function()
gunhold:Play()
local cloneofgui = script.Parent.PistolGui:Clone()
cloneofgui.Parent = player.PlayerGui
local textlabel = cloneofgui.TextLabel
textlabel.Text = "Bullets: "..bulletsleft.Value
bulletsleft.Changed:Connect(function()
textlabel.Text = "Bullets: "..bulletsleft.Value
end)
inputservice.InputBegan:Connect(function(input, gameprocessedevent)
if bulletsleft.Value < 10 then
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.R then
if not reloading then
reloading = true
textlabel.Text = "Reloading..."
reload:Play()
wait(2)
bulletsleft.Value = 10
textlabel.Text = "Bullets: "..bulletsleft.Value
print("Reloaded")
reloading = false
end
end
end
end
end)
mouse.Button1Down:Connect(function()
if bulletsleft.Value > 0 then
script.Parent.Fire:FireServer(mouse.Hit.p)
bulletsleft.Value = bulletsleft.Value - 1
print(bulletsleft.Value)
elseif bulletsleft.Value == 0 then
print("No more bulelts left")
textlabel.Text = "No more bullets! Press R to reload"
inputservice.InputBegan:Connect(function(input, gameprocessedevent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.R then
if not reloading then
reloading = true
textlabel.Text = "Reloading..."
reload:Play()
wait(2)
bulletsleft.Value = 10
textlabel.Text = "Bullets: "..bulletsleft.Value
print("Reloaded")
reloading = false
end
end
end
end)
end
end)
end)
script.Parent.Unequipped:Connect(function()
player.PlayerGui:FindFirstChild("PistolGui"):Destroy()
gunhold:Stop()
end)
Server script handling the shooting aspect:
local canshoot = true
script.Parent.Fire.OnServerEvent:Connect(function(player, mousepos)
if canshoot then
canshoot = false
local raycastparams = RaycastParams.new()
raycastparams.FilterDescendantsInstances = {player.Character}
raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
local direction = (mousepos - script.Parent.Shoot.Position).Unit*100
local raycastresult = game.Workspace:Raycast(script.Parent.Shoot.Position, direction)
local intersection = raycastresult and raycastresult.Position or script.Parent.Shoot.Position + direction
local distance = (script.Parent.Shoot.Position - intersection).magnitude
local bullet = game.ServerStorage.Bullet:Clone()
bullet.Size = Vector3.new(0.1,0.1,distance)
bullet.CFrame = CFrame.new(script.Parent.Shoot.Position, intersection)*CFrame.new(0,0,-distance/2)
bullet.Parent = game.Workspace
if raycastresult then
local hitpart = raycastresult.Instance
local model = hitpart:FindFirstAncestorOfClass("Model")
if model then
if model:FindFirstChild("Zombie") then
model.Zombie:TakeDamage(20)
end
end
end
wait(0.25)
bullet:Destroy()
canshoot = true
end
end)
I thought that whatever code is in the equipped function only happens in the equipped function, but when the gun is unequipped, the code from the equipped function still runs. What did I do wrong?