banner



How To Make A Animation Play By Pressing A Key Roblox

You've got a lot of the necessary pieces already in place. This whole question is generally a matter of making sure that the chain of events happen correctly, and the different events are observed.

The main piece that you are missing how to "grab" another player. And to do that, all you take to do is :

  • Create a weld or constraint between the grabber and the grabbed.
  • Set the grabbed humanoid'southward state to Ragdoll or Physics.
  • Fix the network ownership of grabbed character model to the server.

And that should be enough for you to be able to manipulate the other actor's character. The important matter to know is where these events need to be processed. Some things like animations need to be played on the clientside, while other things similar collision detection need to be managed by the server.

Here is a breakdown of each of the actions from your question :

Event Action Customer-side Server-side
A player on the Smiler team clicks their mouse... spammed input is debounced x
an animation plays x
collisions with players on the other squad are detected x
a cooldown starts to debounce additional clicks x
A player is touched... their controls are disabled x
their character ragdolls ten x
a constraint between the grabber and the player is created x
network ownership over their character is given to the grabber x
their health decreases x
they are released afterward a picayune while ten
A player's wellness decreases... if information technology drops too depression, they switch teams 10
A player is released from the grab... their grapheme no longer ragdolls x 10
the constraint connecting them to the grabber is deleted ten
network ownership is returned to them x
they regain control of their character x

Each step in the process isn't difficult on its own, simply it can be a petty confusing when y'all put it all together. So let's dig into what that might look like.

This instance has a workspace that looks similar this :

Workspace Notes
see notes in next column. - In ReplicatedStorage, at that place are two RemoteEvents for communicating between the server and client. There is an Animation instance holding onto the grab animation.

- In ServerScriptService, there is a Script hosting the server-side logic.

- In StarterPlayer > StarterCharacterScripts, there is a LocalScript hosting the customer-side logic.

- In Teams, at that place are two Team objects divers, ane for the Smilers and i for the other team.

In the LocalScript :

          -- Services local ContextActionService = game:GetService("ContextActionService") local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local Teams = game:GetService("Teams")  -- Remote Events local GrabPlayerEvent = ReplicatedStorage:FindFirstChild("GrabPlayerEvent") local TogglePlayerControlsEvent = ReplicatedStorage:FindFirstChild("TogglePlayerControlsEvent")  -- Animations local GrabAnimation = ReplicatedStorage:FindFirstChild("GrabAnimation")  -- define some variables local actor = Players.LocalPlayer local playerControlModule = require(thespian.PlayerScripts.PlayerModule) local cooldown = false local COOLDOWN_TIMER = 2.5 --seconds -- TODO : implement a UI then that people can encounter this cooldown  -- load the animation local humanoid = role player.Graphic symbol.Humanoid local animator = humanoid:FindFirstChildOfClass("Animator") if non animator then     error("Could not find Animator on character model") end  local animationTrack = animator:LoadAnimation(GrabAnimation) animationTrack.Looped = false animationTrack.Stopped:Connect(function()     -- tell the server that we're done playing the animation     GrabPlayerEvent:FireServer(false) finish)   office onAction(actionName, inputState, inputObject)     -- escape if the grapheme doesn't be     if thespian.Character == nix then         render     end          -- escape if the player isn't on the Smiler team     if player.TeamColor ~= Teams.Smilers.TeamColor then         return     end          -- look for the mouseUp event     if inputState ~= Enum.UserInputState.End and then         return     finish          -- escape if on cooldown     if cooldown and then         return     end          -- Play the animation, and tell the server we're doing it        GrabPlayerEvent:FireServer(true)     animationTrack:Play()           -- commencement the cooldown     cooldown = true     wait(COOLDOWN_TIMER)     cooldown = false end  -- Mind for Mouse clicks to know when to play the grab animation game.ContextActionService:BindAction("Grab", onAction, faux, Enum.UserInputType.MouseButton1)   -- Listen for when the server demands that we enable/disable player controls  TogglePlayerControlsEvent.OnClientEvent:Connect(function(enabled)     -- disable thespian controls     local controls = playerControlModule:GetControls()     if enabled so         controls:Enable()     else         controls:Disable()     terminate          -- ragdoll the grapheme     local graphic symbol = player.Graphic symbol     if not character then         return     stop     local humanoid = graphic symbol.Humanoid      if not enabled then         humanoid:ChangeState(Enum.HumanoidStateType.Physics)     else         humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)     terminate finish)                  

And in the server Script :

          -- Services local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local Teams = game:GetService("Teams")  -- script connections local animationConnections = {} -- array<playerId, array<RBXScriptConnection>>  -- remote events local GrabPlayerEvent = ReplicatedStorage:FindFirstChild("GrabPlayerEvent") local TogglePlayerControlsEvent = ReplicatedStorage:FindFirstChild("TogglePlayerControlsEvent")  -- helper functions  local function constrainTwoParts(partA, partB, constraintType)     local a0, a1 = Case.new("Attachment"), Instance.new("Attachment")     a0.Parent = partA     a1.Parent = partB      local b = Instance.new(constraintType)     b.Attachment0 = a0     b.Attachment1 = a1     b.Parent = game.Workspace          return b stop  local function destroyConstraintsAndAttachments(constraint)     constraint.Enabled = false     constraint.Attachment0:Destroy()     constraint.Attachment1:Destroy()     constraint:Destroy() end  local function createPlayerKey(player)     -- helper function to ensure lexicon central consistency     return tostring(player.UserId) end  local function getPlayerHands(characterModel)     -- detect the role player'due south hands, regardless of R6, R15, or Rthro model type     local easily = {}     local expectedHandNames = { "LeftHand", "LeftArm", "RightHand", "RightArm" }     for _, partName in ipairs(expectedHandNames) do         local paw = characterModel:FindFirstChild(partName, false)         if manus then             tabular array.insert(hands, paw)         end     end          return hands end  local function toggleNetworkOwnership(characterModel, isEnabled, owner)     for _, child in ipairs(characterModel:GetDescendants()) practice         if kid:IsA("BasePart") then             if child.Anchored then                 kid.Anchored = fake             cease                          if isEnabled and then                 child.Massless = fake                 child:SetNetworkOwner(owner)             else                 kid.Massless = true                 kid:SetNetworkOwner(nil)             finish         end     cease terminate  local office enableRagdoll(characterModel, player, enabled)     if enabled then         if player then             -- disable the opponent'due south controls temporarily and ragdoll them             TogglePlayerControlsEvent:FireClient(role player, fake)                          -- set the network ownership to the server to permit better ragdolling             toggleNetworkOwnership(characterModel, imitation, actor)         end                  -- create a bunch of temporary attachments and constraints then they bomb effectually         for _, v in pairs(characterModel:GetDescendants()) do  --ragdoll             if v:IsA("Motor6D") then                 local a0, a1 = Example.new("Zipper"), Instance.new("Attachment")                 a0.CFrame = v.C0                 a1.CFrame = v.C1                 a0.Parent = v.Part0                 a1.Parent = v.Part1                  local b = Instance.new("BallSocketConstraint")                 b.Attachment0 = a0                 b.Attachment1 = a1                 b.Parent = v.Part0                  -- disable the existing character motors                 v.Enabled = false             cease         finish     else         -- remove the ragdoll constraints and connections         for _,v in pairs(characterModel:GetDescendants()) practise  --unragdoll             if v:IsA('Motor6D') then                 v.Enabled = true             end             if v.Proper name == 'BallSocketConstraint' then                 v:Destroy()             end             if v.Name == 'Attachment' and so                 v:Destroy()             end         end                  if player so             -- tell the histrion to cease ragdolling and restore controls             TogglePlayerControlsEvent:FireClient(actor, true)                          -- return network ownership to the original player             toggleNetworkOwnership(characterModel, true, player)         end     stop finish  local role stopObservingHands(role player)     -- clean upwardly the listeners on the player'south hands     local playerKey = createPlayerKey(player)     for _, connection in ipairs(animationConnections[playerKey]) practise         connexion:Disconnect()     terminate stop  local function createHandTouchListener(hand, player)     return function(otherPart)         local grapheme = otherPart.Parent         local humanoid = character:FindFirstChild("Humanoid")         if humanoid then             local otherPlayer = Players:GetPlayerFromCharacter(character)                          if otherPlayer then                 -- check what team the other actor is then y'all don't catch people on your own team                 if otherPlayer.Team == Teams.Smilers and then                     return                 cease             cease                          -- disable the standoff detectors on the easily             stopObservingHands(player)                          -- ragdoll the other player             enableRagdoll(graphic symbol, otherPlayer, true)                              -- add a temporary weld between the manus and the otherPart and then we grab them             local constraint = constrainTwoParts(paw, otherPart, "RodConstraint")             constraint.Enabled = true             constraint.Length = constraint.CurrentDistance             constraint.Thickness = 2                          -- TODO : effigy out how long to hold on here             -- define some constants             local TIME_TO_HOLD_PLAYER = 5.0 -- seconds             local DAMAGE_PER_TICK = 1.0             local TICKS_PER_SECOND = v.0             local TIME_TO_WAIT = one.0 / TICKS_PER_SECOND             local TEAM_SWITCH_THRESHOLD = 0 -- health                          -- start holding the other player and subtracting wellness             local startingTime = tick()             local timePassed = 0.0             local otherPlayerDead = false             while (timePassed < TIME_TO_HOLD_PLAYER and otherPlayerDead == simulated) do                 timePassed = tick() - startingTime                                  -- every tick subtract some wellness from the opponent                 local humanoid = grapheme.Humanoid                 if humanoid so                     -- if this tick will kill the role player, release the weld so we also don't die                     if humanoid.Health - DAMAGE_PER_TICK <= TEAM_SWITCH_THRESHOLD then                         destroyConstraintsAndAttachments(constraint)                         constraint = zippo                     end                      humanoid:TakeDamage(DAMAGE_PER_TICK)                                          -- check if the other player should switch teams                     if (humanoid.Wellness <= TEAM_SWITCH_THRESHOLD) then                         -- modify the player's team to be the smilers                         if otherPlayer and then                             otherPlayer.Team = Teams.Smilers                         stop                         otherPlayerDead = truthful                         break                     end                 else                     otherPlayerDead = truthful                     break                 end                                  wait(TIME_TO_WAIT)             end              -- permit go of the other histrion             if constraint so                 destroyConstraintsAndAttachments(constraint)             end              -- tell the other player to stand up upwardly             enableRagdoll(graphic symbol, otherPlayer, false)         end     stop end  -- Heed for when the client starts to practise the take hold of animation GrabPlayerEvent.OnServerEvent:Connect(office(player, isStart)     local playerKey = createPlayerKey(player)     local playerCharacterModel = player.Character     if playerCharacterModel == nix then         error(string.format("%s'south character model should exist, simply it doesn't", histrion.Name))     finish          -- if this is the start of the blitheness...     if isStart and so         -- set up up listeners in case we bear on the other players         for _, hand in ipairs(getPlayerHands(playerCharacterModel)) do             local touchListener = createHandTouchListener(hand, player)             local connection = paw.Touched:Connect(touchListener)             table.insert(animationConnections[playerKey], connection)         end     else         stopObservingHands(thespian)     terminate terminate)  -- listen for when players join to initialize some variables Players.PlayerAdded:Connect(part(player)     -- add an entry in the connections and animation tables for this player     local fundamental = createPlayerKey(player)     animationConnections[key] = {}          thespian.CharacterAdded:Connect(office(grapheme)         character.Humanoid.BreakJointsOnDeath = false         character.Humanoid.RequiresNeck = imitation     terminate) end)  -- listen for when players leave to make clean upwardly some variables Players.PlayerRemoving:Connect(function(histrion)     -- clean upwardly any connections     local key = createPlayerKey(player)          for _, connection in ipairs(animationConnections[fundamental]) practice         connection:Disconnect()     terminate finish)                  

Source: https://stackoverflow.com/questions/69507339/how-does-one-go-about-making-it-so-that-you-can-grab-other-players-by-clicking-o

Posted by: bowlertheabsitters.blogspot.com

0 Response to "How To Make A Animation Play By Pressing A Key Roblox"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel