UIS works too many times on server script (Or RemoteEvent Problem)

uis works too many times on a server script probaly due to the remote event firing to many times

local script: (prints 1 time)

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, proceseed)
	
	if input.KeyCode == Enum.KeyCode.R then 
		game:GetService("ReplicatedStorage").Remotes.KeyBindEvent:FireServer(UIS:GetStringForKeyCode(input.KeyCode))
		print("kingjacksam01 has pressed R local script")
	end
	
end)

server script: (prints 4 times)

game:GetService("ReplicatedStorage").Remotes.KeyBindEvent.OnServerEvent:Connect(function(player, key)
	print(player.Name.." has pressed "..key.." key")
	
	if key.Name == "R" then
		reload(true, false)
	end


end)

feel free to request images, etc

how many times do you want it to work

it should only work once

AAAAAAAAAAAAAAAAAAAAAAAAA

1 Like

try add debunce to it

like

local debunce = true

Remote function
if debunce = true then
debunce = false
print()
else
debunce = true
print()
end
end

After putting both scripts alongside the remote event into my Roblox studio it appears to work fine are you sure this is what’s causing the problem? is there any other pieces of code that could corelate to the issue?

1 Like

I mean if you want to see the whole server script here it is

local weapon = script.Parent

local rp = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players") -- Add this line
local _settings = require(weapon.Settings)

local canShoot = weapon:WaitForChild("gunEnabled")
local gunReloading = weapon:WaitForChild("gunReloading")

local rayPoint = weapon:WaitForChild("RayPoint")
local reload = false

local ammoEvent = rp.Remotes:WaitForChild("AmmoEvent")
local reloadEvent = rp.Remotes:WaitForChild("ReloadEvent")
local stopReloadEvent = rp.Remotes:WaitForChild("StopReloadEvent")

local gunFireRate = _settings.gunFireRate
local gunDamage = _settings.gunDamage

local ammoCapacity = _settings.gunAmmoCapacity
local clipCapacity = _settings.gunClipCapacity
local currentAmmo = _settings.currentAmmo

local reloadTime = _settings.reloadTime

local currentAmmoCapacity = _settings.gunClipCapacity

local gunGUI = weapon.UIScript.GunGUI
local gunGUIText = gunGUI.AmmoFrame.CurrentAmmoTextLabel

currentAmmo = ammoCapacity

canShoot.Value = true

local radius = 20 -- Radius to search for HumanoidRootParts
local snapDistance = 10 -- Maximum distance to snap the ray to HumanoidRootPart

-- Function to find the nearest HumanoidRootPart within the specified radius (excluding other players)
local function findNearestHumanoidRootPart(origin, radius)
	local nearestHumanoidRootPart = nil
	local shortestDistance = radius

	for _, descendant in ipairs(workspace:GetDescendants()) do
		if descendant:IsA("BasePart") and descendant.Name == "HumanoidRootPart" and not Players:GetPlayerFromCharacter(descendant.Parent) then
			local distance = (descendant.Position - origin).Magnitude
			if distance <= radius and distance < shortestDistance then
				nearestHumanoidRootPart = descendant
				shortestDistance = distance
			end
		end
	end

	return nearestHumanoidRootPart
end

weapon.Activated:Connect(function()
	print("shot")

	if canShoot.Value then
		wait()
		print(currentAmmoCapacity)
		canShoot.Value = false

		print("ammo capacity " .. clipCapacity)

		local rayOrigin = rayPoint.CFrame.Position
		local rayDirection = rayPoint.CFrame.LookVector

		local nearestHumanoidRootPart = findNearestHumanoidRootPart(rayOrigin, radius)

		if nearestHumanoidRootPart then
			local direction = (nearestHumanoidRootPart.Position - rayOrigin).Unit * snapDistance
			rayDirection = direction
		end

		local rayParams = RaycastParams.new()
		rayParams.FilterDescendantsInstances = {weapon}
		rayParams.FilterType = Enum.RaycastFilterType.Exclude

		local raycastResult = workspace:Raycast(rayOrigin, rayDirection * _settings.gunRayDistance, rayParams)

		wait(0.05)

		if currentAmmo > 0 then
			
			currentAmmo -= 1
			print(currentAmmoCapacity)

			-- Fire the RemoteEvent to the client
			ammoEvent:FireAllClients(currentAmmo, currentAmmoCapacity)
			
		end

		if raycastResult then
			local part = raycastResult.Instance
			local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")

			if humanoid then
				humanoid:TakeDamage(gunDamage)
			end
		end

		wait(gunFireRate)

		if currentAmmo > 0 then
			canShoot.Value = true
		else
			canShoot.Value = false
		end

	end
end)

function ammoFunc()
	currentAmmo -= 1
	print(currentAmmoCapacity)

	-- Fire the RemoteEvent to the client
	ammoEvent:FireAllClients(currentAmmo, currentAmmoCapacity)
end

function reload(isMagazineReload, isSingleBulletReload)
	print("IS it 0? : "..currentAmmo)

	if (currentAmmo < clipCapacity or currentAmmo == 0) and isSingleBulletReload and not isMagazineReload and currentAmmoCapacity ~= 0 then
		canShoot.Value = false

		print("Clip Capacity reload(): "..clipCapacity)

		while currentAmmo < clipCapacity and currentAmmoCapacity > 0 do
			print("Reloading")

			reloadEvent:Fire()

			task.wait(reloadTime)

			currentAmmo += 1
			currentAmmoCapacity -= 1

			-- Fire the RemoteEvent to the client
			ammoEvent:FireAllClients(currentAmmo, currentAmmoCapacity)
		end

		canShoot.Value = true
	end

	if (currentAmmo < clipCapacity or currentAmmo == 0) and isMagazineReload and not isSingleBulletReload and currentAmmoCapacity ~= 0 then
		canShoot.Value = false

		print("Clip Capacity reload(): "..clipCapacity)

		print("Reloading")

		task.wait(reloadTime)

		currentAmmoCapacity -= _settings.gunAmmoCapacity
		
		print("Reload")

		-- Fire the RemoteEvent to the client
		ammoEvent:FireAllClients(currentAmmo, currentAmmoCapacity)

		canShoot.Value = true
	end
end

game:GetService("ReplicatedStorage").Remotes.KeyBindEvent.OnServerEvent:Connect(function(player, key)
	
	print(player.Name.." has pressed "..key.." key")
	
	if key.Name == "R" then
		reload(true, false)
	end


end)

1 Like

I’ve been able to count that you’ve called the ammoEvent in someway 4 times exactly so perhaps it’s to do with that seeing as the issue consists of it printing 4 times

(called as in ammoEvent:FireAllClients(currentAmmo, currentAmmoCapacity) ).

ok i disabled another script, which is the same, and now its 2.

Final local script code:

local UIS = game:GetService("UserInputService")

local isUIS = true

UIS.InputBegan:Connect(function(input, proceseed)
	
	if input.KeyCode == Enum.KeyCode.R and UIS == true then 
		UIS = false
		game:GetService("ReplicatedStorage").Remotes.KeyBindEvent:FireServer(UIS:GetStringForKeyCode(input.KeyCode))
		print("kingjacksam01 has pressed R local script")
		UIS = true
	end
	
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.