Part doesn't gives damage with Touched Event - lua

I made a code for my game, it's a type of punch, with a hitbox made with a common Part. I proggrammed to the part give some damage according with the player strenght. It didn't work, no errors in output. 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 = 1
hitbox.Anchored = true
hitbox.CanCollide = false
hitbox.Size = Vector3.new(2.5,2.5,2.5)
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 h = hit.Parent:FindFirstChild("Humanoid")
if h then
if h ~= Humanoid and debounceDMG then
debounceDMG = false
h.Health -= plr.Data.Forca.Value + 5
task.wait(.5)
debounceDMG = true
end
end
end)
task.wait(.3)
hitbox:Destroy()
end)

You use "if h~=Humanoid" will always look for Non Humanoid, your code will never reach the Health deduction. Try to code below if working
hitbox.Touched:Connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil and debounceDMG then
debounceDMG = false
h.Health -= plr.Data.Forca.Value + 5
task.wait(.5)
debounceDMG = true
end
end)

Related

My weapon only works on NPC's and not players

So basically I made a weapon it flings people after you hit them but it only works on NPC's and not players. Could someone explain to me why this is?
local power = 5
local function fling(character)
local humanoid = character:FindFirstChildOfClass("Humanoid")
local rootpart
if humanoid then
humanoid:ChangeState(11)
rootpart = humanoid.RootPart
end
if not rootpart then
rootpart = character.AssemblyRootPart
end
if rootpart then
rootpart.Velocity = Vector3.new(math.random(-100, 100), math.random(50, 100), math.random(-100, 100)) * 10
end
end
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local slash = script:WaitForChild("Slash")
local debounce = false
local idle = false
local PlayersHit = {}
local playerService = game:GetService("Players")
local plr = playerService:GetPlayerFromCharacter(tool.Parent)
local Datastore2 = require(game.ServerScriptService:WaitForChild("Modules").Datastore2)
local defaulthits = 0
local hitds = Datastore2("hits",plr)
tool.Activated:Connect(function()
if idle == false and debounce == false then
debounce = true
local humanoid = tool.Parent:WaitForChild("Humanoid")
local animtrack = humanoid:LoadAnimation(slash)
animtrack:Play()
wait(1)
debounce = false
idle = false
end
end)
handle.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= tool.Parent then
if debounce == true and PlayersHit[hit.Parent] == nil then
fling(hit.Parent)
hitds:Increment(1, defaulthits)
debounce = false
script.Parent.Handle.hit:Play()
end
end
end)
This is my current code. I do not know what to do I'm pretty sure its something to do with the fling script.
I found out how instead of humanoid:changestate(11) i used humanoid.PlatformStand = true

How i can make the parts of a Model be together with primarypart and follow it

I want to make a vfx model primary part go to the Player Character Primary Part, but the parts of the vfx model isn't moving with the model primary part. I tried to weld them but didnt work. What can i do? Code below:
local replicated = game.ReplicatedStorage
local powers = replicated.Powers
local ss = game.ServerStorage
local FireWind = ss.FireWind:Clone()
powers.GiantFire.OnServerEvent:Connect(function(Player, Hit)
local char = Player.Character or Player.CharacterAdded:Wait()
local hrpp = char.PrimaryPart
local SuperFireSphere = Instance.new("Part")
SuperFireSphere.Shape = Enum.PartType.Ball
SuperFireSphere.Material = Enum.Material.Neon
SuperFireSphere.BrickColor = BrickColor.new("New Yeller")
SuperFireSphere.Anchored = true
SuperFireSphere.CanCollide = false
task.wait(.2)
hrpp.Position += Vector3.new(0, 50 , 0)
hrpp.Anchored = true
SuperFireSphere.Parent = workspace
FireWind.PrimaryPart.Position = hrpp.Position
FireWind.Parent = workspace
SuperFireSphere.Position = char.PrimaryPart.Position + Vector3.new(0,15,0)
for i = 1, 10 do
task.wait(.1)
SuperFireSphere.Size += Vector3.new(2,2,2)
end
task.wait(10)
SuperFireSphere:Destroy()
hrpp.Anchored = false
end)
You should try set the PrimaryPartCFrame using model:SetPrimaryPartCFrame(cframe)
In this case, you want it to follow the HumanoidRootPart CFrame.
local model = workspace.Model
local char = game.Players.LocalPlayer:WaitForChild('Character')
local hrp = char.HumanoidRootPart
local part = workspace.Part
model:SetPrimaryPartCFrame(hrp.CFrame) -- sets the model's primary part's CFrame to the hrp.
Local potato = 10
Local TestAmount = 11
If potato == 10 then
Script.parent.cancollide = false
End
If TestAmount == 11 then
Script.parent.transparency = false
end

Two Codes Confliting each other, how to fix it?

I made 2 codes of special abilitys and if i activate the SuperFireSphere code, then the Combat code damage stop working, the hitbox don't spawn in workspace and i don't know how to fix it.
SuperFireSphere Code:
local ts = game:GetService("TweenService")
local replicated = game.ReplicatedStorage
local onepiecepowers = replicated.OnePiecePowers
local Info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local ss = game:GetService("ServerStorage")
local FireWind = ss.FireWind:Clone()
local db = true
onepiecepowers.AceGiantFire.OnServerEvent:Connect(function(Player, Hit)
local char = Player.Character or Player.CharacterAdded:Wait()
local SuperFireSphere = Instance.new("Part")
local hrpp = char.PrimaryPart
local currentcam = workspace.Camera
currentcam.CameraSubject = char.Humanoid
currentcam.CameraType = Enum.CameraType.Scriptable
SuperFireSphere.Shape = Enum.PartType.Ball
SuperFireSphere.Material = Enum.Material.Neon
SuperFireSphere.BrickColor = BrickColor.new("New Yeller")
SuperFireSphere.Anchored = true
SuperFireSphere.CanCollide = false
task.wait(.2)
local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.Velocity = CFrame.lookAt(hrpp.CFrame.Position, Hit.Position).LookVector * 150
hrpp.Position += Vector3.new(0, 50 , 0)
hrpp.Anchored = true
SuperFireSphere.Parent = workspace
FireWind:SetPrimaryPartCFrame(hrpp.CFrame)
FireWind.Parent = workspace
SuperFireSphere.Position = char.PrimaryPart.Position + Vector3.new(0,15,0)
for i = 1, 15 do
task.wait(.1)
SuperFireSphere.Size += Vector3.new(2,2,2)
end
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
local tween = ts:Create(parts, Info, {Transparency = 1})
tween:Play()
end
end
local connect = SuperFireSphere.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= char.Humanoid and db == true then
db = false
hit.Parent.Humanoid.Health -= char.DevilFruitPoints.Value
task.wait(2)
db = true
end
end)
local FireParticle = ss.Fire:Clone()
FireParticle.Parent = SuperFireSphere
SuperFireSphere.Anchored = false
bv.Parent = SuperFireSphere
task.wait(2)
hrpp.Anchored = false
FireWind.Parent = ss
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
parts.Transparency = 0
end
end
task.wait(5)
SuperFireSphere:Destroy()
connect:Disconnect()
end)
Combat:
local replicated = game:GetService("ReplicatedStorage")
local combatevent = replicated.Combat
local servstore = game:GetService("ServerStorage")
local runservice = game:GetService("RunService")
local db = true
combatevent.OnServerEvent:Connect(function(Player)
local char = Player.Character or Player.CharacterAdded:Wait()
local hbox = Instance.new("Part")
hbox.Size = Vector3.new(1.5,1.5,1.5)
hbox.Anchored = true
hbox.CanCollide = false
local connection = runservice.Heartbeat:Connect(function(dt)
hbox.Position = char.RightHand.Position
end)
local tconnection = hbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= char.Humanoid and db then
db = false
hit.Parent.Humanoid.Health -= char.Strenght.Value
task.wait(.1)
db = true
end
end)
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://10497535355"
animation.Parent = char.Humanoid
local loadanim = char.Humanoid:LoadAnimation(animation)
loadanim:Play()
hbox.BrickColor = BrickColor.new("Really red")
hbox.Transparency = 0.5
hbox.Name = Player.Name
print(hbox.Name)
task.wait(.2)
hbox:Destroy()
tconnection:Disconnect()
loadanim:Stop()
connection:Disconnect()
end)
Does someone have any suggestion to help me to fix that??
.........................................................

How to make a code that shakes the camera of the player

I tried to make a code that's shake the camera player when a RemoteEvent is fired by a key on the keyboard. But it didn't worked. I tried to use the CameraOffset but didn't worked too. How can i make this code without using the CameraShakeModule??? Any sugestion??? Code below:
local ts = game:GetService("TweenService")
local replicated = game.ReplicatedStorage
local powers = replicated.Powers
local Info = TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local ss = game:GetService("ServerStorage")
local FireWind = ss.FireWind:Clone()
powers.GiantFire.OnServerEvent:Connect(function(Player, Hit)
local char = Player.Character or Player.CharacterAdded:Wait()
local SuperFireSphere = Instance.new("Part")
local hrpp = char.PrimaryPart
local currentcam = workspace.Camera
currentcam.CameraSubject = char.Humanoid
currentcam.CameraType = Enum.CameraType.Scriptable
SuperFireSphere.Shape = Enum.PartType.Ball
SuperFireSphere.Material = Enum.Material.Neon
SuperFireSphere.BrickColor = BrickColor.new("New Yeller")
SuperFireSphere.Anchored = true
SuperFireSphere.CanCollide = false
task.wait(.2)
local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.Velocity = CFrame.lookAt(hrpp.CFrame.Position, Hit.Position).LookVector * 150
hrpp.Position += Vector3.new(0, 50 , 0)
hrpp.Anchored = true
SuperFireSphere.Parent = workspace
FireWind:SetPrimaryPartCFrame(hrpp.CFrame)
FireWind.Parent = workspace
SuperFireSphere.Position = char.PrimaryPart.Position + Vector3.new(0,15,0)
for i = 1, 15 do
task.wait(.1)
SuperFireSphere.Size += Vector3.new(2,2,2)
end
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
local tween = ts:Create(parts, Info, {Transparency = 1})
tween:Play()
end
end
for i = 1,20 do
local x = math.random(-100, 100)/100
local y = math.random(-100, 100)/100
local z = math.random(-100, 100)/100
char.Humanoid.CameraOffset = Vector3.new(x,y,z)
print(i)
task.wait(.5)
end
local FireParticle = ss.Fire:Clone()
FireParticle.Parent = SuperFireSphere
SuperFireSphere.Anchored = false
bv.Parent = SuperFireSphere
task.wait(2)
hrpp.Anchored = false
FireWind.Parent = ss
for i, parts in pairs(FireWind:GetDescendants()) do
if parts:IsA("MeshPart") then
parts.Transparency = 0
end
end
task.wait(5)
SuperFireSphere:Destroy()
end)
Your problem is that you made the CameraType scriptable, with it scriptable, the character's Humanoid no longer has control of it. Remove the lines that make the CameraType scriptable and it will solve your issue.

while loop wont work inside of the other while loop

I'm scripting in roblox lua and when I try to put a while loop in the first it messes up the first sequence of code can someone give me hand in fixing this?
The problem is that when the humanoid makes it to tar2 he stops there and wont move I want him to continue running to the points but while also having it so where if a actual person comes by then he runs after him instead.
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
local plr = game.Players:GetChildren()
local tar1 = game.Workspace.tar1
local tar2 = game.Workspace.tar2
local tar3 = game.Workspace.tar3
local tar4 = game.Workspace.tar4
local pos = {tar1, tar2, tar3, tar4}
local char = script.Parent
local Hum = char.Humanoid
torso = char.Torso
GoTo = 0
while true do
wait(1)
if GoTo == 0 then
Hum:MoveTo(pos[1].Position)
end
if (torso.Position - pos[1].Position).magnitude < 5 then
GoTo = 1
end
if GoTo == 1 then
Hum:MoveTo(pos[2].Position)
end
if (torso.Position - pos[2].Position).magnitude < 5 then
GoTo = 2
end
if GoTo == 2 then
Hum:MoveTo(pos[3].Position)
Goto = 3
end
if (torso.Position - pos[3].Position).magnitude < 5 then
GoTo = 4
end
if GoTo == 5 then
Hum:MoveTo(pos[4].Position)
end
while true do
wait(0.1)
plrs = game.Players:GetChildren()
for i,plr in ipairs(plrs) do
if plr.Character ~= nil then
tor = plr.Character.Torso
if (torso.Position-tor.Position).magnitude <= 5 then
GoTo = 0
Hum:MoveTo(tor.Position)
end
end
end
end
end
Coroutines! Coroutines are the bombdiggety. They let you do multiple while loops at once in a single script.
local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")
local plr = game.Players:GetChildren()
local tar1 = game.Workspace.tar1
local tar2 = game.Workspace.tar2
local tar3 = game.Workspace.tar3
local tar4 = game.Workspace.tar4
local pos = {tar1, tar2, tar3, tar4}
local char = script.Parent
local Hum = char.Humanoid
torso = char.Torso
GoTo = 1
function normalCor() -- run around
while true do
wait(1)
for i=1, 4 do
if GoTo == i then
Hum:MoveTo(pos[i].Position)
end
if (torso.Position - pos[i].Position).magnitude < 5 then
GoTo = GoTo + 1
if GoTo == 5 then
GoTo = 1
end
--[[
The above _can_ be done in a single statement:
"GoTo = (GoTo+1)%4+1"
"a % 4" gets the remainder of a / 4 ]]
end
end
end
end
cr = coroutine.create(normalCor)
coroutine.resume(cr) -- this lets you do two while loops at the same time.
function aggroCor() -- when a player is nearby
while true do
wait(0.1)
plrs = game.Players:GetChildren()
for i,plr in ipairs(plrs) do
if plr.Character ~= nil then
tor = plr.Character.Torso
if (torso.Position-tor.Position).magnitude <= 5 then
GoTo = 0
Hum:MoveTo(tor.Position)
end
end
end
end
end
cr = coroutine.create(aggroCor)
coroutine.resume(cr)
Roblox has the syntax spawn(aggroCor) that you can play around with too,
if coroutine.whatever seems like too much work.

Resources