I'm making a script where a part will increase its size and while its increasing, it can hurt players the problem is, while its increasing it goes above the player and any other player that touches it. The part is anchored and can collide is false. Why is this happening?
He's a gif so you can see whats happening: https://media.giphy.com/media/T7moZi0R4zX2g/giphy.gif
Code (Local script, starterpack):
local plr = game.Players.LocalPlayer local stats = plr:WaitForChild("leaderstats") local e = stats:WaitForChild("EXP") local d = stats:WaitForChild("damage") local canfire = true function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.Z then if canfire then canfire = false plr.Character.Torso.Anchored = true local animTrack = plr.Character.Humanoid:LoadAnimation(script.Animation) animTrack:Play() local x = Instance.new("Part") x.Size = Vector3.new(0.5,0.5,0.5) x.BrickColor = BrickColor.new("Bright blue") x.Material = "Sand" x.CFrame = plr.Character.Torso.CFrame + Vector3.new(0,-3,0) x.Anchored = true x.CanCollide = false x.Parent = game.Workspace for i = 1,25 do wait(0.01) x.Size = x.Size + Vector3.new(5,0,5) end x.Touched:connect(function(hit) local ehum = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid") if ehum and ehum ~= plr.Character.Humanoid then e.Value = e.Value + 1 ehum:TakeDamage(d.Value + 1) end end) plr.Character.Torso.Anchored = false wait(1) x:Remove() canfire = true end end end game:GetService("UserInputService").InputBegan:connect(onKeyPress)
A few things will not work. Here is what the script should look like, and explained:
local plr = game.Players.LocalPlayer local stats = plr:WaitForChild("leaderstats") local e = stats:WaitForChild("EXP") local d = stats:WaitForChild("damage") local canfire = true function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.Z then if canfire then canfire = false plr.Character.Torso.Anchored = true local animTrack = plr.Character.Humanoid:LoadAnimation(script.Animation) animTrack:Play() local x = Instance.new("Part") x.Size = Vector3.new(0.5,0.5,0.5) x.BrickColor = BrickColor.new("Bright blue") x.Material = "Sand" x.CFrame = plr.Character.Torso.CFrame *CFrame.new(0,-3,0) x.Anchored = true x.CanCollide = false x.Parent = game.Workspace for i = 1,25 do wait(0.01) x.Size = x.Size + Vector3.new(5,0,5) x.CFrame = plr.Character.Torso.CFrame *CFrame.new(0,-3,0) end local EBounce = true x.Touched:Connect(function(hit) -- capital Connect local human = hit.Parent:FindFirstChild("Humanoid") -- check for the right humanoid if human ~= nil and hit.Parent ~= plr.Name and EBounce then -- if it's a player/npc and it's not the player that casted it, and add a debounce EBounce = false e.Value = e.Value + 1 human:TakeDamage(d.Value + 1) wait(0.5) EBounce = true end end) plr.Character.Torso.Anchored = false game.debris:AddItem(x, 5) -- this makes x stay for 5 seconds wait(1) canfire = true end end end game:GetService("UserInputService").InputBegan:connect(onKeyPress)