In my gun script, I have an implemented check to see if the player has the gun equipped, if they don’t the script kicks them.
Here’s the problem, for some reason, it also kicks them if the player clicks the mouse button WITHOUT the gun equipped.
Local script code:
-- Main Variables --
local tool = script.Parent.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local event = script.Parent:WaitForChild("FireEvent")
-- Function --
mouse.Button1Down:Connect(function()
event:FireServer(mouse.Hit.Position,player)
end)
Server script:
-- Main Variables --
local debris = game:GetService("Debris")
local event = script.Parent:WaitForChild("FireEvent")
local gun = script.Parent
local handle = script.Parent.Handle
-- Configuration --
local BulletSpeed = 500
local BulletDMG = 5
-- Function --
event.OnServerEvent:Connect(function(player, pos)
print("Input received, engaging check..")
if player.Character:FindFirstChild(gun.Name) then -- check if gun equipped
local char = player.Character
-- Projectile --
print("Check successful!")
local bullet = Instance.new("Part")
bullet.Parent = game.Workspace
bullet.BrickColor = BrickColor.new("Bright yellow")
bullet.Material = Enum.Material.Neon
bullet.Size = Vector3.new(0.795, 0.59, 2.235)
bullet.Position = handle.Position
bullet.CanCollide = false
bullet.CFrame = CFrame.lookAt(bullet.Position, pos)
print("Bullet is created.")
-- Movement --
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.Parent = bullet
BodyVelocity.Velocity = bullet.CFrame.LookVector * BulletSpeed
BodyVelocity.P = BulletSpeed
print("Bullet is moving.")
-- Bullet Lifetime --
debris:AddItem(bullet, 5)
-- Hit Detection --
local connection
connection = bullet.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent == gun.Parent then
print("Wielding player hit, ignoring..")
else
print("Player hit!")
hit.Parent.Humanoid.Health -= BulletDMG
end
elseif hit:IsA("Part") then
if hit == handle then
print("Touched Handle")
else
bullet:Destroy()
end
end
end)
else
print("Check unsuccessful.")
player:Kick("Check failed, security measures engaged.")
print(player.Name .. " has been kicked.")
end
end)