I have a problem with my Roblox combat system. The problem is that my Animations don't play when I play them. Here are the scripts:
Client Script
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local UserInputService = game:GetService("UserInputService")
local Debounce = 0.5
local Keybind = Enum.KeyCode.F
local CanPunch = true
local count = 1
local Animations =
{
script:WaitForChild("PunchAnim"),
script:WaitForChild("PunchAnim2")
}
UserInputService.InputBegan:Connect(function(Input, busy)
if Input.KeyCode == Keybind and not busy then
print("Keybind Check")
if CanPunch == true then
print("CanPunch Check")
CanPunch = false
local Anim = char.Humanoid.Animator:LoadAnimation(Animations[count])
Anim:Play()
Anim.Looped = false
count = (count%#Animations) + 1
print("Anim Played")
game.ReplicatedStorage.remotes.Punch:FireServer(player, char)
print("Fired Event")
wait(Debounce)
CanPunch = true
end
end
end)
Server Script
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
hitbox = Instance.new("Part", workspace)
hitbox.Size = Vector3.new(4,4,4)
hitbox.CanCollide = false
hitbox.Transparency = 1
local weld = Instance.new("Weld", hitbox)
weld.Part0 = char.HumanoidRootPart
weld.Part1 = hitbox
weld.C1 = CFrame.new(0,0,4)
end)
end)
game.ReplicatedStorage.remotes.Punch.OnServerEvent:Connect(function(player, char)
for i, v in pairs(workspace:GetPartsInPart(hitbox)) do
if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= char and v.Parent:FindFirstChild("Hit"..player.Name) == nil then
local Debounce = Instance.new("IntValue", v.Parent)
Debounce.Name = "Hit"..player.Name
game.Debris:AddItem(Debounce, 0.25)
v.Parent:FindFirstChild("Humanoid"):TakeDamage(7.5)
end
end
end)
In the script, I'm telling the Animations to play after F is pressed but when I press it in the game, it doesn't play the animation.
I made this script by watching various tutorials to make my own combat system. I've tried fixing the code by re-watching the tutorials as well as reading the roblox documentation to find a solution but I couldn't find one.
I'm new to Lua and I apologize in advance if this is a very easy and basic question, but all help is appreciated. Also, please point out any other mistake in my script. Thanks.
If you see this question, please answer it if you can.
The answer of my question was, that the animations were R6 but my character was R15. I went and changed the game settings and made it so the game only supported R6.
Related
I made a punch script, with a hitbox part, but it doesn't gives damage and i have an error in the output called "Attempt to index boolean with 'Health'", but the humanoid exists and his properties too, what is happening? Can i fix that? Code here:
local rep = game:GetService("ReplicatedStorage")
local debounceDMG = true
rep.Combate.Soco.OnServerEvent:Connect(function(plr)
local math = math.random(1,2)
local char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = char.Humanoid
local lanim = char.Humanoid:LoadAnimation(script.Left)
local lanim1 = char.Humanoid:LoadAnimation(script.Animation)
local hitbox = Instance.new("Part")
hitbox.Parent = workspace
hitbox.Transparency = 0
hitbox.Anchored = true
hitbox.CanCollide = false
hitbox.Size = Vector3.new(2.5,2.5,2.5)
hitbox.CanTouch = true
local c = game:GetService("RunService").Heartbeat:Connect(function()
if math == 2 then
hitbox.Position = char.LeftHand.Position
elseif math == 1 then
hitbox.Position = char.RightHand.Position
end
end)
if math == 2 then
lanim:Play()
elseif math == 1 then
lanim1:Play()
end
hitbox.Touched:Connect(function(hit)
local hum = hit.Parent:FindFirstChild("Humanoid") ~= Humanoid
if hum then
hum.Health -= 5
end
end)
task.wait(.3)
hitbox:Destroy()
c:Disconnect()
end)
This line:
local hum = hit.Parent:FindFirstChild("Humanoid") ~= Humanoid
assigns a boolean to hum, depending on whether the return value from FindFirstChild is not equal to Humanoid.
Because hum is a boolean, that's why you get the Attempt to index boolean with 'Health' runtime error on this line:
hum.Health -= 5
First, I'd recommend adding this hot comment to the very top of your script:
--!strict. Then, Luau LSP (if you're using Visual Studio Code) and/or Roblox Studio can tell you about this mistake before your code even runs. It's really nice to see the red squiggle appear and tell you more precisely what's wrong.
Second, you probably mean for the hum assignment to simply be:
local hum = hit.Parent:FindFirstChild("Humanoid")
I'm trying to make a cutscene sorta thing for a dorr in roblox studio. My solution was to set up a collision detector on the door which would then make a gui template and set its parent to the playergui component.
I did this using the code
local afterIntoTransform = script.Parent.Parent.DoorUnion.Position.Z -6
local afterOutwardsTransform = script.Parent.Parent.DoorUnion.Position.Z + 6
local debounce = false
local function executeFadeSceneAndTpPlayer(player)
local fadeScene = Instance.new("ScreenGui")
local fadeSceneFrame = Instance.new("Frame")
fadeScene.Name = "fadeScene"
fadeSceneFrame.Name = "fadeFrame"
fadeSceneFrame.Size = UDim2.new(1,0,1,0)
fadeSceneFrame.Parent = fadeScene
fadeSceneFrame.BorderSizePixel = 0
fadeSceneFrame.BackgroundColor3 = Color3.new(1, 1, 1)
fadeSceneFrame.BackgroundTransparency = 1
print(game.Players:GetPlayerFromCharacter(player).Name)
fadeScene.Parent = game.Players:GetPlayerFromCharacter(player).PlayerGui
for i = 0, 20, 1 do
fadeSceneFrame.BackgroundTransparency -= 0.05
wait(0.01)
end
player.HumanoidRootPart.Position = Vector3.new(player.HumanoidRootPart.Position.X, player.HumanoidRootPart.Position.Y, afterOutwardsTransform)
for i = 0, 20, 1 do
fadeSceneFrame.BackgroundTransparency += 0.05
wait(0.01)
end
fadeScene:Destroy()
end
script.Parent.Touched:Connect(function(hit)
if not debounce then
debounce = true
executeFadeSceneAndTpPlayer(hit.Parent)
wait(0.2)
debounce = false
end
end)
It tells me: Attempted to index nil with name on line 15.
It works sometimes and sometimes doesnt but recently Ive noticed a trend that I can walk into the door then out again and then it breaks. I haven't coded in a while so I'm a little rusty but I hope I can get some help.
You are running into the same issue as this person : Roblox - attempt to index nil with 'leaderstats'
You are not accounting for the fact that the Touched event fires for every single part that touches it, and some of those parts might not belong to a player.
You can protect against this error by making sure that the object belongs to a player before you call executeFadeSceneAndTpPlayer()
script.Parent.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if not plr then
return
end
if not debounce then
debounce = true
executeFadeSceneAndTpPlayer(hit.Parent)
wait(0.2)
debounce = false
end
end)
I have created this script in hopes of creating a combat system that can play multiple animations in one button; however, when I put them in the light attack section of the script, the animations will not play, yet I have no errors in my code.
I have tried reorganizing, using the actual animation ids, changing variable names, etc.
local Player = game.Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character.Humanoid
AnimationId1 = "rbxassetid://2046787868"
AnimationId2 = "rbxassetid://2046881922"
AnimationId3 = "rbxassetid://"
AnimationId4 = "rbxassetid://2048242167"
Debounce = true
local Key = 'U'
local Key2 = 'I'
local Key3 = 'O'
local Key4 = 'P'
local UserInputService = game:GetService("UserInputService")
--Animation for the Light attk combo sequence.
UserInputService.InputBegan:connect(function(Input, IsTyping)
for i, v in pairs(game.Players:GetChildren()) do
if Input.KeyCode == Enum.KeyCode[Key] then
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationId1, AnimationId2
local LoadAnimation = Humanoid:LoadAnimation(Animation)
if v == 1 then
LoadAnimation:Play(AnimationId1)
elseif v == 2 then
LoadAnimation:Play(AnimationId2)
end
end
end
end)
--Animation for the Blocking sequence.
UserInputService.InputBegan:connect(function(Input, IsTyping)
if IsTyping then return end
if Input.KeyCode == Enum.KeyCode[Key4] and Debounce then
Debounce = false
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationId4
local LoadAnimation = Humanoid:LoadAnimation(Animation)
LoadAnimation:Play()
wait(.5)
LoadAnimation:Stop()
Debounce = true
end
end)
The blocking part of this script works perfectly, however, when I try to use the light attack section, it doesn't work.
In your light attack function, v is a Player object. So any check like v == 1 or v == 2 will fail because it is the wrong type. It also doesn't really make sense that you would iterate over all of the players when they press the 'U' button.
You can make it play an animation just like you did with your blocking animation code.
-- make a counter to help decide which animation to play
local swingCount = 0
local currentSwingAnimation
--Animation for the Light attack combo sequence.
UserInputService.InputBegan:connect(function(Input, IsTyping)
if IsTyping then return end
if Input.KeyCode == Enum.KeyCode[Key] then
swingCount = swingCount + 1
-- cancel any animation currently playing
if currentSwingAnimation then
currentSwingAnimation:Stop()
currentSwingAnimation = nil
end
if swingCount == 1 then
-- play the first animation
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationId1
currentSwingAnimation = Humanoid:LoadAnimation(Animation)
currentSwingAnimation.Looped = false
currentSwingAnimation:Play()
elseif swingCount == 2 then
-- play the second swing animation
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationId2
currentSwingAnimation = Humanoid:LoadAnimation(Animation)
currentSwingAnimation.Looped = false
currentSwingAnimation:Play()
-- reset the swing counter to start the combo over
swingCount = 0
end
end
end)
My first thought was to ask on Roblox devforum but since imo they got a really messed up admission system, I might as well ask it here.
I've got a tool that shoots a block (wedge) to wherever the mouse is pointing when clicked. It also casts a ray and the block itself sets the health of any humanoid that makes contact with it to 0. But I have got no idea on how to actually implement a cooldown on the gun so you can't just literally spam blocks that kill anything that touches them arround. I think implementing a debounce here is the best option, but I got stuck with that since day 1 and I have no idea how to write it down correctly
I already tried with most of the things that I thought of after visiting this page Roblox dev page about Debounce, also read through some articles that had similar issues in the dev forum, but I can just spam blocks arround whatever I do.
The tool has just two parts (one being the handle) a localscript to wield the parts together, a localscript to catch the mouse position when clicked, two remote events to pass the info from the localscript to the server script and the following server script
local tool = script.Parent
local clickEvent = tool.ClickEvent
local clickEventConnection
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
--Function that creates the part with a touched listener that kills any humanoid that comes into contact with said block
local function createPart(location)
local part = Instance.new("WedgePart")
part.CFrame = location
part.Parent = workspace
part.BrickColor = BrickColor.new("Black")
part.Touched:connect(function(hit)
if hit.Parent then
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum then
hum.Health = 0
end
end
end)
game:GetService("Debris"):AddItem(part, 2)
end
--With the information on the click position of the localscript, this function creates a ray and a beam that accompanies the block, as well as executing the createpart() function on said location
local function onClick(player, clickLocation, ignore)
createPart(clickLocation)
local ray = Ray.new(
tool.Handle.CFrame.p,
(clickLocation.p - tool.Handle.CFrame.p).unit * 500
)
local hit, position, normal = workspace:FindPartOnRay(ray, player.Character, ignore)
local beam = Instance.new("Part", workspace)
if player.Team == Teams["Blue Team"] then
beam.BrickColor = BrickColor.new("Bright blue")
elseif player.Team == Teams["Red Team"] then
beam.BrickColor = BrickColor.new("Bright red")
else
beam.BrickColor = BrickColor.new("Ghost grey")
end
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 1)
end
--subscribing onclick() when equiping the weapon and unsubscribig when unequipping it
local function onEquip()
clickEventConnection = clickEvent.OnServerEvent:connect(onClick)
end
local function onUnequip()
clickEventConnection:disconnect()
end
tool.Equipped:connect(onEquip)
tool.Unequipped:connect(onUnequip)
I just wanted to make a 'cooldown' so a block can be fired each 3 seconds. As is, you can just spam as much as you like to
A simple way to debounce the clicks is to use a variable to decide whether to quickly escape from a function.
You can modify your onClick function so it does not execute if a cooldown is still in place :
-- make a cooldown tracker
local isGunOnCooldown = false
local cooldownTime = 3.0 --seconds
local function onClick(player, clickLocation, ignore)
-- debounce any spammed clicks
if isGunOnCooldown then
return
end
-- put the gun on cooldown
isGunOnCooldown = true
-- fire a bullet
createPart(clickLocation)
local ray = Ray.new(
tool.Handle.CFrame.p,
(clickLocation.p - tool.Handle.CFrame.p).unit * 500)
local hit, position, normal = workspace:FindPartOnRay(ray, player.Character, ignore)
local beam = Instance.new("Part", workspace)
if player.Team == Teams["Blue Team"] then
beam.BrickColor = BrickColor.new("Bright blue")
elseif player.Team == Teams["Red Team"] then
beam.BrickColor = BrickColor.new("Bright red")
else
beam.BrickColor = BrickColor.new("Ghost grey")
end
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (tool.Handle.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, 1)
-- start the gun's cooldown and reset it
spawn(function()
wait(cooldown)
isGunOnCooldown = false
end)
end
My code:
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Activation =
Instance.new("Sound",game.Players.LocalPlayer.Character.Head)
local char = Player.Character
local hum = char.Humanoid
local root = char.HumanoidRootPart
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://1581972610"
local animTrack = hum:LoadAnimation(animation)
animTrack:Play()
Activation.SoundId = "rbxassetid://1581091676" --Plays Mangekyou Sharingan Activation Sound.
Activation:Play()
wait(0.3)
game.Players.LocalPlayer.Character.Head.face.Texture = "rbxassetid://76285632" --When F is pressed, face texture changes to sharingan decal.
game:GetService("Chat"):Chat(Player.Character.Head, "Mangekyou Sharingan!")
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
Activation.SoundId = "rbxassetid://1580990602" --Plays Amaterasu Activation Sound.
Activation:Play()
game:GetService("Chat"):Chat(Player.Character.Head, "Amaterasu!")
local Target = Instance.new("Part") --makes a part
Target.CFrame = Mouse.Hit; --Makes part spawn at the mouse's current location in game
Target.Parent = game.Workspace
Target.Transparency = 1
Target.Anchored = true
Target.CanCollide = false
local Amaterasu = Instance.new("Fire")
Amaterasu.Parent = game.Workspace.Part
Amaterasu.Color = Color3.new(0,0,0)
Amaterasu.SecondaryColor = Color3.new(0,0,0) --amaterasu properties
Amaterasu.Size = 25
local R = Instance.new("RocketPropulsion") --rocket propulsion, parents amaterasu
R.Parent = Amaterasu
R.MaxThrust = 300
R.ThrustP = 30
R:Fire()
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.G then
game.Players.LocalPlayer.Character.Head.face.Texture = "rbxassetid://22557247" --When G is pressed, face texture changes back to normal.(leaves face blank isnt working :/)
end
end)
I am working on the second function in this script, the one that activates if the "r" key is pressed. The function makes a part spawn to the mouses current location with flames inside of it by pressing the "r" key.
This works all fine. I want the flames that spawn to damage any player it touches for a certain amount of health, in this case I want the damage to be 100 health.
I believe what you're looking for is Touched - see Creating a Dangerous Trap: https://wiki.roblox.com/index.php?title=Creating_Traps_and_Pickups
Example provided in documentation:
local trapPart = script.Parent
local function onPartTouch(otherPart)
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
if ( humanoid ) then
-- Set player's health to 0
humanoid.Health = 0
end
end
trapPart.Touched:Connect(onPartTouch)
But disclaimer - not a Roblox dev (just bugging one right now).
Best of luck.
Don't make your parent before setting properties otherwise it causes performance lag