I want to make a tool that when you get hit, you start to sleep. I already have the tool model and animations working, but i cant figure out how to make the other person do the animation. I tried a lot of methods but none of them work. Its kind of like when you step on a part an animation plays, but instead of stepping on it you get hit by it.
This is my script
local hum = script.Parent.Parent.Humanoid
local anim = hum:LoadAnimation(script.Parent.zzzanimation)
You need to use remote events. Touching the part is processed on the server, but in order to play an animation you have to use a localscript. I think reading this doc will help you, especially the âShould I load an Animation on the client or server?â.
A partâs âTouchedâ signal will fire on the client, thereâs no need for any server intervention if it isnât required.
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Part = Workspace.Part
local Toggle = false
local function OnTouched(TouchedPart)
if Toggle then return end
local TouchedModel = TouchedPart:FindFirstAncestorOfClass("Model")
if not TouchedModel then return end
local TouchedHumanoid = TouchedModel:FindFirstChildOfClass("Humanoid")
if (not TouchedHumanoid) or TouchedHumanoid.Health <= 0 then return end
local TouchedAnimator = TouchedHumanoid:FindFirstChildOfClass("Animator")
if not TouchedAnimator then return end
local TouchedPlayer = Players:GetPlayerFromCharacter(TouchedModel)
if not TouchedPlayer or (TouchedPlayer ~= Player) then return end
Toggle = true
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://656118852" --Ninja run animation.
local Track = TouchedAnimator:LoadAnimation(Animation)
Track.Looped = true
Track:Play()
end
Part.Touched:Connect(OnTouched)
Touching the part is processed on the server, but in order to play an animation you have to use a localscript.
Both of these claims are however false, the touching of a part can be processed by the client and the server is able to load and play animations (even if it shouldnât do).
The OP is implying there are two players involved; in which case, .Touched should be done on the server.
I disagree, the client has network ownership over their own character, if the touched event simply results in the loading and subsequent playing of an animation then everything should be handle by the client.
I want to make a tool that when you get hit, you start to sleep.
They just want to make the hit playerâs character perform a âsleepâ animation.
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid") or Character:WaitForChild("Humanoid")
local Animator = Humanoid:FindFirstChildOfClass("Animator") or Humanoid:WaitForChild("Animator")
local function OnHumanoidTouched(TouchingPart, HumanoidPart)
if TouchingPart.Name == "Projectile" then --Check if touching part is a 'projectile'.
--Load and play animation here.
end
end
Humanoid.Touched:Connect(OnHumanoidTouched)