Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

starter character animation script not working? (continuation)

Asked by
pwgth 11
3 years ago

so hey! it's me, some random guy who's bad at scripting, pwgth. so a bit ago, I made a question: "Why do our scripts only work for ourselves?" which a very kind person, gave us an answer on why.

so afterward, I and my friend composed brand new scripts! (which don't work) and that's why I'm here. Does anybody know what I did wrong? I put these both in starter character scripts btw.

local script:

local mouse = game.Players.LocalPlayer:GetMouse()

local rep = game.ReplicatedStorage

mouse.KeyDown:connect(function(key)
    if key=="f" then 
        rep.Punch:FireServer()
    end
end)

script:

local rep = game.ReplicatedStorage

local animation = rep.Folder["punchy boi"]

local trackanimation = nil
local playability = true

rep.punch.OnServerEvent:Connect(function(player)
    if playability == true then
        local plr = game.Players.LocalPlayer
        trackanimation = plr.Character.Humanoid:LoadAnimation (animation)

        trackanimation.KeyframeReached:Connect(function()
            print("10!")
        end)
        trackanimation:play()
    end
end)

I highlighted the stuff I thought was important to make sure yall didn't miss out on em lol.

peace! -pwgth

0
you don't need to reference the localplayer since you already did in the client script, since :FireServer() returns the player JesseSong 3916 — 3y
0
on line 11 try: trackanimation = player.Character.Humanoid:LoadAnimation(animation) JesseSong 3916 — 3y
0
also, the other script should be a normal script placed in either serverscriptservice or workspace JesseSong 3916 — 3y
0
i might post an answer soon JesseSong 3916 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

This code is all over the place. Make sure the name of the thing in replicstedStorage has one constant name. In the local script I see rep.Punch and in the server-side script I see rep.punch, the names are different Second, please use waitforchild so the script doesn't throw errors and stop, things like rep:WaitForChild"Punch". Third, don't use key down, and I think it's deprecated anyway, although I could be wrong. Use UserinputService.InputBegan, it's supposedly better, also I'm pretty sure the Key name always has to be uppercase. In your script, it checks for the key "f", it's gotta be "F" or if your using user input use Enum.KeyCode.F. fourth, why in tarnation are you setting local player in a SERVERSCRIPT especially when you already HAVE the player instance!?!? It's there, "player", so use it please. Also, isn't key frame reached supposed to include the name of the key frame? Like animtrack.KeyFrameReached("Name"). I could be wrong though and it would work with the anim if it has only 1 keyframe, but please, use the name just in case you ever add more key frames to the anim in the future, it's good to think ahead, always makes coding easier.. Please learn to script before trying to script. Read forums and articles and stuff.

I hope this helps :3

0
Yea, UIS is better than mouse.KeyDown. JesseSong 3916 — 3y
0
animtrack.KeyFrameReach requies a connection and a function. ref: https://developer.roblox.com/en-us/api-reference/event/AnimationTrack/KeyframeReached JesseSong 3916 — 3y
0
man that was harsh... and then you put a ":3" face LOL. welp, this will help. hopefully it'll work pwgth 11 — 3y
0
it doesn't matter if it's mouse.KeyDown "f" it will still work JesseSong 3916 — 3y
View all comments (4 more)
0
also, mouse.KeyDown "F" wouldn't work. JesseSong 3916 — 3y
0
Im sorry if i was wrong i was tired on on mobile so i typed without thinking. Also im sorry it seemed harsh, i didnt mean it like that  AlexanderYar 788 — 3y
0
I know how it feels trust me. When you answer questions but get it wrong especially on mobile, then someone corrects you. I've experienced that many times. This is why I never go on mobile anymore, since there's multiple benefits of using a PC, than mobile. JesseSong 3916 — 3y
0
Of course AlexanderYar 788 — 3y
Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Problem:

You're using two local scripts in a remote event. Remote events are used for client-server communications it cannot work. If you want to communicate from client-to-client or server to server then use a RemoteFunction.

Improvements:

  • Use :Play() instead of :play() as that isn't a built-in function.
  • Use UserInputService instead of mouse.KeyDown, cause that is deprecated.
  • :connect is deprecated, use:Connect instead (with an upercase c)

Fixed Code:

Client: (Place in StarterPlayer<StarterPlayerScripts or StarterCharacterScript)

local ReplicatedStorage = game.ReplicatedStorage
local punch = ReplicatedStorage:WaitForChild("RemoteEvent")
local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.F then
        punch:FireServer()
    end
end)

Server-Script: (Place in ServerScriptService or workspace)

local ReplicatedStorage = game.ReplicatedStorage
local punch = ReplicatedStorage:WaitForChild("RemoteEvent") -- this is the remote event. make sure it's placed in replicatedstorage
local animation = ReplicatedStorage.Folder["punchy boi"]

local trackanimation = nil
local playability = true

punch.OnServerEvent:Connect(function(player)
    if playability  then
        trackanimation = player.Character.Humanoid:LoadAnimation (animation)

        trackanimation.KeyframeReached:Connect(function()
            print("10!")
        end)
        trackanimation:Play()
    end
end)

References:

Remote Events- TheDevKing

Remote Events- API Documentation

(I'm pretty sure this code will run, if it does or doesn't let me know!)

0
man, I think this will work, Thanks! pwgth 11 — 3y
0
btw I read it, one of them is a reg script pwgth 11 — 3y
0
alright, but if my answer helped you, please accept it. JesseSong 3916 — 3y
0
wait, what do you mean by "one of them is a reg script" is it the links that i provided, or my script? JesseSong 3916 — 3y

Answer this question