Punch animation from Roblox tutorial is throwing errors - lua

I'm trying to get a punch animation to work from a tutorial, but it never works when I press the designated Keybinding.
I have tried changing values from the tutorial from 1 to the animation length, past the animation length, and even random numbers. I also tried changing some wording to the script.
math.randomseed(tick())
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"
local animation = (03910055905)
local function onPunchFired(plr)
local char = game.Workspace:FindFirstChild(plr.Name)
local humanoid = char.Humanoid
local animation = Instance.new("Animation")
animation.AnimationId = "http://roblox.com/asset/?id="..animations[math.random(1, #animations)]
local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()
end
punchEvent.OnServerEvent:Connect(onPunchFired)
I expect the script to run smooth and punch, but it shows this:
20:46:29.496 - ServerScriptService.ExtremePunch:13: attempt to get length of global 'animations' (a nil value)
This is all it says in the output for errors. I've double checked the replication, but it keeps failing. How can I fix it?

Your error is pointing at this line :
animation.AnimationId = "http://roblox.com/asset/?id="..animations[math.random(1, #animations)]
This is because in the scope of this script, there is no local variable or array named animations.
To fix your code, you just have to fix the number in the animation.AnimationId url. One problem you might have run into was that you had two variables named animation so it failed to construct the url properly for you.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)
punchEvent.Name = "PunchEvent"
local function onPunchFired(plr)
local char = game.Workspace:FindFirstChild(plr.Name)
local humanoid = char.Humanoid
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://3910055905"
local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()
end
punchEvent.OnServerEvent:Connect(onPunchFired)
If you're unsure if your asset id is correct, you can also check by putting it into the url for the Roblox Catalog : https://www.roblox.com/catalog/YOUR_ASSET_ID_NUMBER. If it redirects to your animation, you know you've got the right one.

The animations list does not exist in the code that you showed us. So, add your other punch animations to the animations list that I added and you should be good to go. I didn't test it so if it's not working, HMU.
local replicatedStorage = game:GetService("ReplicatedStorage")
local punchEvent = Instance.new("RemoteEvent")
punchEvent.Name = "PunchEvent"
punchEvent.Parent = replicatedStorage
local animations = [03910055905, ...]
local function onPunchFired(plr)
local char = game.Workspace:FindFirstChild(plr.Name)
local humanoid = char.Humanoid
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. tostring(animations[math.random(1, #animations)])
local animTrack = humanoid:LoadAnimation(animation)
animTrack:Play()
end
punchEvent.OnServerEvent:Connect(onPunchFired)

Related

how to make the camera offset relative to the object in the roblox studio via a script?

i cannot make offset of camera in roblox
there is no errors in output but its anyways dont works
task.wait(3)
print('hvnrherjwl')
local runService = game:GetService('RunService')
local cam = game.Workspace.CurrentCamera
local camAttachment = script.Parent
local offset = camAttachment.Position + Vector3.new(25,40,25)
local lookAt = camAttachment.Position
cam.CameraType = 'Scriptable'
runService.Heartbeat:Connect(function(deltaTime)
cam.CFrame = CFrame.new(offset,lookAt)
end)
p.s. i used runService to modify it in the future

Can Someone Help Me Make A Script that When you click a TextButton It runs the script?

I wanted to make a Gui that when you press a TextButton to size your BodyWidth, BodyDepth, Head and BodyHeight and also change your walkspeed nad jumpower. I wanted it to understand when you clicked the button and run the script but I can't do it alone.
Here is the script:
--The error is here
game.Players.PlayerAdded:Connect(function(Player)
--Maybe here too
Player.CharacterAdded:Connect(function(Character)
local Humanoid = Character.Humanoid
Humanoid.BodyDepthScale.Value = 10
Humanoid.BodyHeightScale.Value = 10
Humanoid.BodyWidthScale.Value = 10
Humanoid.HeadScale.Value = 10.5
Humanoid.WalkSpeed = 70
Humanoid.JumpPower = 0
end)
end)
end)
I tried to make an if condition But I still couldn't do the script.
First add a LocalScript to the button, and type:
local LocalPlayer = game:GetService("Players").LocalPlayer
local char = LocalPlayer.Character
local Humanoid = char:WaitForChild("Humanoid")
function Click ()
Humanoid.WalkSpeed = 70
Humanoid.JumpPower = 0
-- scale the meshes right here like char.Head.Size = Vector3.new(3,3,3)
end
script.Parent.MouseButton1Up:Connect(Click)
You can scale the meshes not the Humanoid Values

When ever I run this music script it works except for changing the GUI text to the current song name

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mutebutton = script.Parent
local track = game.StarterGui.MusicPlayer.Track
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local c = math.random(1, #b)
local d = b[c]
while true do
wait()
d:Play()
local connection = mutebutton.MouseButton1Click:Connect(function()
if d.IsPaused then
d:Resume()
mutebutton.Text = "Pause"
else
d:Pause()
mutebutton.Text = "Resume"
end
end)
d.Ended:Wait()
connection:Disconnect()
d:GetPropertyChangedSignal("SoundId"):Connect(function()
track.Text = d.Name
end)
end
Everything works until the Get Property Changed Signal event. Whenever I load into the game the box that should have the song name will instead not do anything and keep it as an empty box. Anyone know why?
It seems you have a single mute button (not one for each song), so why are you putting the pausing functionality inside a while loop?
You're not changing the sound id anywhere, is there more code that you're not showing?
You have a variable "local d" but this is only ever calculated outside of the loop so it will just repeat the same song over.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mutebutton = script.Parent
local track = game.StarterGui.MusicPlayer.Track
local b = game.StarterGui.MusicPlayer.Playlist:GetChildren()
local c = math.random(1, #b)
local song -- definite it originally here as nil so we can access it outside of the loop
mutebutton.MouseButton1Click:Connect(function()
if song.IsPaused then
song:Resume()
mutebutton.Text = "Pause"
else
song:Pause()
mutebutton.Text = "Resume"
end
end)
while true do
song = b[math.random(1, #b)]
track.Text = song.Name
song:Play()
song.Ended:Wait()
task.wait()
end
This might work, but I can't see your Explorer

Tried making coin counter, nothing happens when tested

So, I have a game on Roblox currently in development, and I am trying to make a coin counter. Basically, when the coin is collected, a NumberValue in StarterPlayer changes by +1. Then, A TextLabel will loop to check the value of the NumberValue. It will then change the text to display the value of the NumberValue.
However, when I playtest, nothing happens. The counter doesn't change, but the NumberValue does.
Here's the code for the Coin:
local coin = script.Parent
local sp = game:GetService("StarterPlayer")
local count = sp.Coins
local p = game:GetService("Players")
local Touched = false
coin.Touched:Connect(function()
count.Value += 1
local Sparkles = Instance.new("Sparkles")
Sparkles.Parent = coin
Sparkles.SparkleColor = Color3.fromRGB(255, 255, 0)
wait(2)
count.Value += 1
coin:Destroy()
end)
And the code for the TextLabel:
local StarterPlayer = game:GetService("StarterPlayer")
local Coins = StarterPlayer.Coins
local CoinLabel = script.Parent
while true do
CoinLabel.Text = Coins.Value
end
Does anyone know how to fix this?
The reason why your TextLabel is never updating is because you have an infinite loop that causes the script to timeout and stop running.
while true do
CoinLabel.Text = Coins.Value
end
The simple fix for that is to only update the TextLabel when the NumberValue changes :
Coins.Changed:Connect(function()
CoinLabel.Text = Coins.Value
end)
But something else is a little weird too. Objects in StarterPlayer are expected to be copied to each player when they join, but StarterPlayer.Coins is not, and as it is right now, it is acting as a global counter for everyone. So your code is modifying the Coins object that would be copied over to new players, not the one that each player has.
But according to the documentation for StarterPlayer, only a few kinds of objects in StarterPlayer are copied to players.
A StarterPlayerScripts instance, with scripts that run once for each player.
A StarterCharacterScripts instance, with scripts to add to each player’s character every time they spawn.
A Humanoid instance named StarterHumanoid, which will be used as the default humanoid for each player’s character.
A Model instance named StarterCharacter, which will be used as the character model for all players
So the NumberValue wouldn't have been copied to each player even if this logic was set up to locate it properly! So, we need to make a few changes :
A Coins NumberValue needs to be created for each player when they join the game.
You need to update the paths to the individual Coins NumberValues for each player.
Your TextLabel code should listen for the
NumberValue.Changed
signal, that way your TextLabel will only update when the
NumberValue does.
So in a Script somewhere like ServerScriptService, add this to create a Coins NumberValue for each Player :
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
-- give each player a Coins object
local coins = Instance.new("NumberValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = player
end)
Next, update the paths in each script to reflect the new location of the object. In the first script, use the player that touched the coin to locate the Player object :
local coin = script.Parent
local Players = game:GetService("Players")
local Touched = false
coin.Touched:Connect(function(otherPart)
-- make sure a player touched this
if otherPart.Parent:FindFirstChild("Humanoid") == nil then
return
end
-- only call this function once
if Touched then
return
end
Touched = true
-- get the player
local player = Players:GetPlayerFromCharacter(otherPart.Parent)
if not player then
return
end
-- increment the player's coin counter
player.Coins.Value += 1
-- show some sparkles
local Sparkles = Instance.new("Sparkles")
Sparkles.SparkleColor = Color3.fromRGB(255, 255, 0)
Sparkles.Parent = coin
-- destroy the coin after a moment
wait(2)
coin:Destroy()
end)
Finally, update the LocalScript that updates the TextLabel so that it properly finds the NumberValue and updates only when the NumberValue changes :
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Coins = player.Coins
local CoinLabel = script.Parent
Coins.Changed:Connect(function()
CoinLabel.Text = Coins.Value
end)

Roblox script running multiple times?

I need a bit of help. Basically I have this code:
local plyIsEntered = false
function onTouched(hit)
plyIsEntered = true
if not plyIsEntered then
end
if plyIsEntered then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local ply = humanoid.Parent
if humanoid ~= nil then
print("Hit")
local playerName = hit.Parent.Name
print(playerName)
local laserEmitter = game.Workspace["Enterance PC"]:FindFirstChild("laserEmitter")
local scanLaser = Instance.new("Part", game.Workspace)
scanLaser.Position = laserEmitter.Position
scanLaser.Name = "ScanLaser"
scanLaser.Size = Vector3.new(1,1,1)
local scanLaserMesh = Instance.new("SpecialMesh", game.Workspace.ScanLaser)
scanLaserMesh.Name = "Cone mesh"
scanLaserMesh.MeshType = ""
plyIsEntered = false
end
end
end
script.Parent.Touched:connect(onTouched)
Now I'm checking if the player touches a box, it has no collisions and is invisible; when they do I want to create a laser that will scan them and open a door. The problem I'm having is when I walk into the trigger box it creates 8 or 9 blocks. One of those blocks is the block I'm applying a mesh too.
What I need to do is make sure it's only running once and not creating more than 1 brick. Hopefully someone can help me!
I believe to fix this you'll need to add a debounce.
You see, the touched event of a Part actually fires many times, so your code will execute multiple times if it is inside of the event.
To fix this, we use a debounce, which means your code won't execute if your part is touched too much in the same time frame. Here is an example:
local debounce = false
part.Touched:connect(function()
if debounce == false then
debounce = true
--Your code goes here.
wait(1)--Wait one second until you'll be able to execute the code again.
debounce = false
end
end)
To read more on debounces: http://wiki.roblox.com/index.php?title=Debounce

Resources