I can't figure it out

I’m trying to prevent the player from reloading the gun when they are dead, but i can’t figure it out

Here is the code:

local tool = script.Parent
local player = game.Players.LocalPlayer
local shooting = false
local mouse = player:GetMouse()
local raycastfilter = RaycastParams.new()
local character = player.Character or player.CharacterAdded:Wait()
raycastfilter.FilterDescendantsInstances = {character}
raycastfilter.FilterType = Enum.RaycastFilterType.Blacklist
local ammo = 6
local max_ammo = 6
local cooldown = 0.3
local reloading = false
local mouse_icon = "http://www.roblox.com/asset/?id=9720078100"
local UIS = game:GetService("UserInputService")
local StarterGui = game:GetService("StarterGui")
local equipped = false
local CAS = game:GetService("ContextActionService")
local position = UDim2.new(0.939, 0,0.58, 0)

player.CharacterAdded:Connect(function(character)
	raycastfilter.FilterDescendantsInstances = {character}
	raycastfilter.FilterType = Enum.RaycastFilterType.Blacklist
end)

local function reload()
	if not reloading then
		player.PlayerGui.RevolverGUI.TextLabel.Text = "Reloading!"
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
		tool.Handle.Reload:Play()
		reloading = true
		task.wait(2)
		ammo = max_ammo
		player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(max_ammo)
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
		reloading  = false
	end
end

tool.Equipped:Connect(function()
	equipped = true
	player.PlayerGui.RevolverGUI.Enabled = true
	player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(ammo)
	mouse.Icon = mouse_icon
	local bindAction = CAS:BindAction("RELOAD", reload, true, "r")
	CAS:SetPosition("RELOAD", position)
end)

tool.Unequipped:Connect(function()
	equipped = false
	mouse.Icon = ""
	player.PlayerGui.RevolverGUI.Enabled = false
	script.Parent.Handle.Reload:Stop()
	CAS:UnbindAction("RELOAD")
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R and equipped then
		reload()
	end
end)

tool.Activated:Connect(function()
	if not shooting and not reloading and script:FindFirstAncestorWhichIsA("Tool").Parent:FindFirstChildWhichIsA("Humanoid").Health > 0 then
		if ammo == 0 then
			reload()
			return
		end
		ammo -= 1
		tool.Handle.Fire:Play()
		player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(ammo)
		local raycast = workspace:Raycast(tool.Handle.Position,(mouse.Hit.Position - tool.Handle.Position)*300, raycastfilter)
		if raycast then
			local instance = raycast.Instance
			local instanceParent = instance.Parent
			local findHumanoid = instanceParent:FindFirstChildOfClass("Humanoid")

			if findHumanoid then
				game.ReplicatedStorage.RemoteEvent:FireServer(findHumanoid)
			end
		end
		shooting = true
		task.wait(cooldown)
		shooting = false
	end
end)

I actually had this error fixed but it appeared again after i did some changes to fix another glitch. It’s pretty insane, anyways uh hopefully it gets solved

try changing your inputbegan function for R to this

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R and equipped then
        if character.Humanoid:GetState() == Enum.HumanoidStateType.Dead then return end
        if character.Humanoid.Health <= 0 then return end
		reload()
	end
end)

Ok, I’ll try that

This text will be blurred

Did u type the indentation correctly?

Cause it doesn’t do anything

This text will be blurred

wdym it doesnt do anything? it errors?

local tool = script.Parent
local player = game.Players.LocalPlayer
local shooting = false
local mouse = player:GetMouse()
local raycastfilter = RaycastParams.new()
local character = player.Character or player.CharacterAdded:Wait()
raycastfilter.FilterDescendantsInstances = {character}
raycastfilter.FilterType = Enum.RaycastFilterType.Blacklist
local ammo = 6
local max_ammo = 6
local cooldown = 0.3
local reloading = false
local mouse_icon = "http://www.roblox.com/asset/?id=9720078100"
local UIS = game:GetService("UserInputService")
local StarterGui = game:GetService("StarterGui")
local equipped = false
local CAS = game:GetService("ContextActionService")
local position = UDim2.new(0.939, 0,0.58, 0)

player.CharacterAdded:Connect(function(character)
	raycastfilter.FilterDescendantsInstances = {character}
	raycastfilter.FilterType = Enum.RaycastFilterType.Blacklist
end)

local function reload()
	if not reloading then
		player.PlayerGui.RevolverGUI.TextLabel.Text = "Reloading!"
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
		tool.Handle.Reload:Play()
		reloading = true
		task.wait(2)
		ammo = max_ammo
		player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(max_ammo)
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
		reloading  = false
	end
end

tool.Equipped:Connect(function()
	equipped = true
	player.PlayerGui.RevolverGUI.Enabled = true
	player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(ammo)
	mouse.Icon = mouse_icon
	local bindAction = CAS:BindAction("RELOAD", reload, true, "r")
	CAS:SetPosition("RELOAD", position)
end)

tool.Unequipped:Connect(function()
	equipped = false
	mouse.Icon = ""
	player.PlayerGui.RevolverGUI.Enabled = false
	script.Parent.Handle.Reload:Stop()
	CAS:UnbindAction("RELOAD")
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R and equipped then
        if character.Humanoid:GetState() == Enum.HumanoidStateType.Dead then return end
        if character.Humanoid.Health <= 0 then return end
		reload()
	end
end)

tool.Activated:Connect(function()
	if not shooting and not reloading and script:FindFirstAncestorWhichIsA("Tool").Parent:FindFirstChildWhichIsA("Humanoid").Health > 0 then
		if ammo == 0 then
			reload()
			return
		end
		ammo -= 1
		tool.Handle.Fire:Play()
		player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(ammo)
		local raycast = workspace:Raycast(tool.Handle.Position,(mouse.Hit.Position - tool.Handle.Position)*300, raycastfilter)
		if raycast then
			local instance = raycast.Instance
			local instanceParent = instance.Parent
			local findHumanoid = instanceParent:FindFirstChildOfClass("Humanoid")

			if findHumanoid then
				game.ReplicatedStorage.RemoteEvent:FireServer(findHumanoid)
			end
		end
		shooting = true
		task.wait(cooldown)
		shooting = false
	end
end)

Yea man nothing. I can still reload even if im dead

are you using a custom death system?

No i ain’t using anything that is custom

try changing the reload function itself then

local function reload()
	if not reloading then
        if character.Humanoid:GetState() == Enum.HumanoidStateType.Dead then return end
        if character.Humanoid.Health <= 0 then return end
		player.PlayerGui.RevolverGUI.TextLabel.Text = "Reloading!"
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
		tool.Handle.Reload:Play()
		reloading = true
		task.wait(2)
		ammo = max_ammo
		player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(max_ammo)
		StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
		reloading  = false
	end
end

Alright

This text will be blurred

Yes it was the function. Im not understanding something about UIS, I wonder what it is… Thx for the help i guess

1 Like

Tho i have a question: The two if statements have the same conditions?

yes at the end they basically do the same thing. Its just there just incase. It will likely never need to check the the 2nd one

I would have solved this myself, Who knew i had to change the function Lol

But why couldn’t i make it check if the player was alive in the UIS Part?

That’s my question now

Roblox stop adding useless content

because you are calling the function in multiple places

Wdym multiple places?

This text will be blurred

It is only used once

This text will be blurred