Keybinded Cuff system not firing event

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    What i’m currently trying to achieve is figure out to play the animation on the server rather than the client.
  2. What is the issue? Include screenshots / videos if possible!
    https://gyazo.com/47bd842d94e66b9fd9f565c8f7777c0e
    If I play the animation in the script, it will fire multiple times due to the fact there’s a loop that checks whos the closest player.
    2nd, The animation plays on the client only.
repeat wait() until game.Players.LocalPlayer.Character
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local UserInputService = game:GetService("UserInputService")
local HS = false
local clone = script.Parent.Templates.Keybind.Keys
local grabbing = false

local Remote_Event = game.ReplicatedStorage:WaitForChild("Remote_Events")

local grabbingPlayer = nil
local suspect = script.Parent.NearestPlayer.Value

game:GetService("RunService").RenderStepped:Connect(function()
		for i ,v in pairs(game.Players:GetPlayers()) do
			if v.Name ~= LocalPlayer.Name then
				if v.Character and v.Character.HumanoidRootPart then
					if (LocalPlayer.Character.HumanoidRootPart.Position - v.Character.HumanoidRootPart.Position).Magnitude <= 6 then
						local NearestPlayer = v.Name
						script.Parent.NearestPlayer.Value = NearestPlayer
						HS = true
						if HS == true then do
								clone.Adornee = v.Character.Torso
								clone.Enabled = true
								UserInputService.InputBegan:Connect(function(input)
									if input.KeyCode == Enum.KeyCode.C then
										if (LocalPlayer.Character.HumanoidRootPart.Position - v.Character.HumanoidRootPart.Position).Magnitude <= 6 then
											game.SoundService.KeyClick:Play()
											game.ReplicatedStorage.Remote_Events.CuffEvent:FireServer()
											grabbing = true
											wait(1)
										end
									end
								end)
							end
						end
					else
						script.Parent.NearestPlayer.Value = ""
						clone.Enabled = false
						clone.Adornee = script.Parent.Templates.Keybind
				end
			end
		end
	end
end

ServerScriptService:

game.ReplicatedStorage.Remote_Events.RemoteEvent.OnServerEvent:Connect(function(plr)
    local anim = plr.humanoid:LoadAnimation(5647195059)
    anim:play()
end)
2 Likes

Why are you waiting for the remote event? Is it not premade? In that case, use an infrastructure that only runs necessary code when events has all been made. What parts of the code currently works? Any errors to be aware of? If not > Use printing and breakpoints to debug and locate the issue. Help us narrow down to where the issues lie.

Use the localPlayer as a variable,

local player = game.Players.LocalPlayer

don’t loop the ENTIRE playerlist just to find the player. Ineffective.
To get the character use

local character = player.Character or player.CharacterAdded:Wait()

Note: For reference don’t mindlessly run CharacterAdded:Wait() on serverside.

You don’t want to play the animation on the server

Okay, onto the solution… you need to fire an event to the server and then tell the server to replicate the animation for every player. Now, you can either do fireServer > fire:allclients.

And then have a onClientEvent handler on the client. Or you could do a more effective and responsive approach which is firing an event to the server, playing the animation on the client and telling the server to fire to all clients except for the client that sent the event in the first place.

1 Like

I’ve used printing it just wont print on the FireServer, but it’ll go through the rest of the code. And i’m only trying to Animate the player who is nearby the (Officer) in this situation it’s the player whos pressing C.

Please re-read the reply to learn good practice in coding. And read the solutions.

Connecting a even each RenderStepped or ‘HS’ is just dumb, and might cost memory issues.
You are also not passing any argument in ‘CuffEvent’ I don’t know if you have to.
Move the event somewhere else, and make a variable, and put the closest player to the variable.

image
What’s this??
Doesn’t makes sense at all.
‘HS == true then do’ ??

If the player has seen the gui and it’s not near by no one it will Parent back its self to the Folder.

The ends is also wrong.
You are setting HS to true, to check if its true and run?
That’s not needed.
image
the wait(1)
At the end is not needed.
There’s too many stuff you should re-do

It’s a simple debug so players dont spam C and hear it so I added wait(1) to do so?

No that’s not gonna change anything just yield any other event since you are connecting even EACH render.

Not a huge fan of just doing all of the code since MOST people don’t actually learn much from it. So only use this if you can’t execute the concept I’ve proposed earlier.



local function playAnim()
    local humanoid = character:WaitForChild("Humanoid")
    humanoid:LoadAnimation() -- add this

end

UIS.InputBegan:Connect(function() 
    if not character then 
        return 
    end


    if debounce then
        debounce = false
        playAnim()
        remote:FireServer()
        wait(cooldown)
        debounce = true
    end
end)

RunService.Heartbeat:Connect(function() 
    for _, plr in pairs(Players:GetPlayers()) do
        if plr == player then 
            return 
        end

        local body = plr.Character
        local distance = (character.PrimaryPart.Position - body.PrimaryPart.Position).Magnitude

        if not plr.Character then 
            return 
        end

        if not closest then
            closest = distance
        elseif closest > distance then 
            Gui.Adornee = body:FindFirstChild("UpperTorso")
            closest = distance
        end
    end
end)

remote.onClientEvent:Connect(function() 
    playAnim()
end)


--//Server

remote.OnServerEvent:Connect(function(player) 
    for _, plr in pairs(Players:GetPlayers()) do
        if plr == player then 
            return 
        end

        remote:FireClient(plr)
    end
end)

No variables defined cuz of laziness… cmon do some work on ur own >_>

1 Like