Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesn't my anchored part stay anchored?

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)

1 answer

✓ Accepted answer

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)

5 comments

1

sorry that the spacing was not put correct, not sure why it glitched when I clicked post answer

0

Thanks, that's a way better way of doing that, but I'm still having the same problem. The part still goes above the player while its increasing.

0

oh, you want the part to not be above the player? I thought you wanted that, I'll edit it!

0

Okay so now I made it work 3 studs below the torso and remain that way, I think that is what you were trying to do. Accept if it works :)

0

Thanks my guy, this has been a struggle for a long time. Thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.