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

First time the part is created, it welds to the player perfectly. But the next time it doesn't?

I made a script that makes a shield around the player when the key is pressed. It works perfectly fine the first time it's pressed, the part is created and welds it to the player. But the second time the part just falls off from the player and map. Why?

p = game.Players.LocalPlayer
c = p.Character
pressed = false
cooldown = 3 
utilitytime = 10
show = p.PlayerGui:WaitForChild('Powers').Shield

s = Instance.new('Part')
sw = Instance.new('Weld',s)

function onKeyPress(Shield, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin then
		if not pressed then
		pressed = true
		print('Q was pressed')
		s.Parent = workspace
		s.Shape = 'Ball'
		s.Locked = false
		s.Anchored = false
		s.Transparency = 0.5
		s.CanCollide = false
		s.BrickColor = BrickColor.new('Cyan')
		s.Material = 'Neon'
		s.Size = Vector3.new(6,6,6)
		s.CFrame = p.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,0)
		
		sw.Part0 = p.Character.HumanoidRootPart
		sw.Part1 = s
		sw.C0 = CFrame.new(0,0,0)
		
		show.BackgroundColor3 = Color3.new(0,0,0)
		show.Text = 'SHIELD: ON USE'
		end
	end
	wait(utilitytime) show.Text = 'SHIELD: ON USE'
	wait(.4)
	s.BrickColor = BrickColor.new('Bright orange')
	wait(.4)
	s.BrickColor = BrickColor.new('Deep orange')
	wait(.4)
	s.BrickColor = BrickColor.new('Really red')
	wait(.4)
	s:remove()
	show.Text = 'SHIELD: COOLDOWN'
	wait(cooldown)
	show.Text = 'SHIELD (Q)'
	show.BackgroundColor3 = Color3.new(0,0,0)
	pressed = false
end
game.ContextActionService:BindAction('keyPress',onKeyPress,false,Enum.KeyCode.Q)

1 comment

0

Because you removed 's' , when the player press the button again .. it only reads the line starts from 11 or the function

1 answer

✓ Accepted answer


p = game.Players.LocalPlayer
c = p.Character
pressed = false
cooldown = 3 
utilitytime = 10
show = p.PlayerGui:WaitForChild('Powers').Shield



function onKeyPress(Shield, userInputState, inputObject)
	s = Instance.new('Part') -- so the game will read this and create new 's' after you removed it below the function or your cooldown.
	sw = Instance.new('Weld',s)
    if userInputState == Enum.UserInputState.Begin then
        if not pressed then
        pressed = true
        print('Q was pressed')
        s.Parent = workspace
        s.Shape = 'Ball'
        s.Locked = false
        s.Anchored = false
        s.Transparency = 0.5
        s.CanCollide = false
        s.BrickColor = BrickColor.new('Cyan')
        s.Material = 'Neon'
        s.Size = Vector3.new(6,6,6)
        s.CFrame = p.Character.HumanoidRootPart.CFrame * CFrame.new(0,0,0)

        sw.Part0 = p.Character.HumanoidRootPart
        sw.Part1 = s
        sw.C0 = CFrame.new(0,0,0)

        show.BackgroundColor3 = Color3.new(0,0,0)
        show.Text = 'SHIELD: ON USE'
        end
    end
    wait(utilitytime) show.Text = 'SHIELD: ON USE'
    wait(.4)
    s.BrickColor = BrickColor.new('Bright orange')
    wait(.4)
    s.BrickColor = BrickColor.new('Deep orange')
    wait(.4)
    s.BrickColor = BrickColor.new('Really red')
    wait(.4)
    s:remove()
    show.Text = 'SHIELD: COOLDOWN'
    wait(cooldown)
    show.Text = 'SHIELD (Q)'
    show.BackgroundColor3 = Color3.new(0,0,0)
    pressed = false
end
game.ContextActionService:BindAction('keyPress',onKeyPress,false,Enum.KeyCode.Q)

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