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

KeyDown function works in studio but not server?

So i finally finished this script. It was originally a local script but for some reason it wouldn't run on servers only in studio test so now its a normal script. Now I have the same problem again.

local player = script.Parent.Parent
local char = player.Character
local hum = player.Character.Humanoid
local canTrans = true
Torso = char.UpperTorso
local mouse = player:GetMouse() --Will use this later


mouse.KeyDown:connect(function(key)--If b is pressed
    if key == "b" and canTrans == true then
		game:GetService('Chat'):Chat(char.Head,string.upper("i won't let you...!"),1)--make character chat
        canTrans = false--cannot spam b to keep transforming
		hum.WalkSpeed = 0
		hum.MaxHealth = 5000
        hum.Health = 5000
		

        if char:FindFirstChild('Humanoid') then --delete accessories
            for i,v in pairs(char:GetChildren()) do
                if v:IsA'Accoutrement' or v:IsA'Hat' then
                    game.Debris:AddItem(v, .3); 
                end
            end

		part1 = game.ServerStorage.cylinderEX:Clone()--clone cylinder
		
		hum:LoadAnimation(game.ServerStorage.ssjanim):Play()
		
			spawn(function()--Im not really sure what spawn does :P
					part1.Parent = workspace
					part1.Position = char.UpperTorso.Position
					wait()
				
			
			for i = 1,20 do
				part1.Size = part1.Size + Vector3.new(1,1,1)--animate part1
				part1.Transparency = part1.Transparency + .05
				wait()
			end
			part1:Destroy()
			end)
			wait(2)
			ball = game.ServerStorage.ball:Clone()--create a new part
			ball.Parent = workspace
			ball.CFrame = char.UpperTorso.CFrame
			
			spawn(function()
			for i = 1,20 do
				wait()
				ball.Size = ball.Size + Vector3.new(1,1,1)--animate ball
				ball.Transparency = ball.Transparency + .05
				ball.CFrame = char.UpperTorso.CFrame
			end
			ball:Destroy()
		end)

			
			wait(.3)
			game:GetService('Chat'):Chat(char.Head,string.upper("haaaaaaa"),1)--make character chat again
			aura = game.ServerStorage.Aura:Clone()
			hair = game.ServerStorage.SSJHAIR:Clone()--clone the accessory and make character wear it
        	hair.Parent = char
			wait(.7)
			aura.Parent = char.UpperTorso--particleEmitter
			hum.WalkSpeed = 25
			canTrans = false
        end
    end
 end)

mouse.KeyDown:connect(function(key)

	if key == "b" and canTrans == false then--if B is pressed again then
		hum.WalkSpeed = 16
		hum.MaxHealth = hum.MaxHealth / 50
		hum.Health = 100
		char.UpperTorso:FindFirstChild("Aura"):Destroy()--Destroy the particleEmitter

		

		if char:FindFirstChild('Humanoid') then --delete accessories
            for i,v in pairs(char:GetChildren()) do
                if v:IsA'Accoutrement' or v:IsA'Hat' then
                    game.Debris:AddItem(v, .3); 
                end
            end
		wait(5)
		canTrans =  true
         end
     end
end)


The script worked fine when the function was called when the player chatted now when its called on keydown it doesn't run when I create a server.

2 comments

0

Quick tip: you can removes accessories even if they aren't loaded by calling :RemoveAccessories() on a humanoid

0

Thanks :D

2 answers

✓ Accepted answer

Key down is depreciated and I recommend UserInputService or ContextActionService instead

-- This is a local script
local player = script.Parent.Parent
local char = player.Character
local hum = player.Character.Humanoid
local canTrans = true
Torso = char.UpperTorso
local UIS = game:GetService("UserInputService")


UIS.InputBegan:Connect(function(key)--If b is pressed
    if key.KeyCode == Enum.KeyCode.B and canTrans == true then
        game:GetService('Chat'):Chat(char.Head,string.upper("I won't let you...!"),1)--make character chat
        canTrans = false--cannot spam b to keep transforming
        hum.WalkSpeed = 0
        hum.MaxHealth = 5000
        hum.Health = 5000


        if char:FindFirstChild('Humanoid') then --delete accessories
            for i,v in pairs(char:GetChildren()) do
                if v:IsA("Accoutrement") or v:IsA("Hat") then
                    game.Debris:AddItem(v, .3); 
                end
            end

        part1 = game:GetService("ServerStorage").cylinderEX:Clone()--clone cylinder

        hum:LoadAnimation(game.ServerStorage.ssjanim):Play()


                    part1.Parent = workspace
                    part1.Position = char.UpperTorso.Position


            for i = 1,20 do
                part1.Size = part1.Size + Vector3.new(1,1,1)--animate part1
                part1.Transparency = part1.Transparency + .05
                wait()
            end
            part1:Destroy()
            wait(2)
            ball = game.ServerStorage.ball:Clone()--create a new part
            ball.Parent = game.Workspace
            ball.CFrame = char.UpperTorso.CFrame

            for i = 1,20 do
                wait()
                ball.Size = ball.Size + Vector3.new(1,1,1)--animate ball
                ball.Transparency = ball.Transparency + .05
                ball.CFrame = char.UpperTorso.CFrame
            end
            ball:Destroy()
            wait(0.3)
            game:GetService('Chat'):Chat(char.Head,string.upper("haaaaaaa"),1)--make character chat again
            aura = game:GetService("ServerStorage").Aura:Clone()
            hair = game:GetService("ServerStorage").SSJHAIR:Clone()--clone the accessory and make character wear it
            hair.Parent = char
            wait(0.7)
            aura.Parent = char.UpperTorso--particleEmitter
            hum.WalkSpeed = 25
            canTrans = false
        end
    end
 end)

UIS.InputBegan:Connect(function(key)

    if key == Enum.KeyCode.B and canTrans == false then--if B is pressed again then
        hum.WalkSpeed = 16
        hum.MaxHealth = hum.MaxHealth / 50
        hum.Health = 100
        char.UpperTorso:FindFirstChild("Aura"):Destroy()--Destroy the particleEmitter



        if char:FindFirstChild('Humanoid') then --delete accessories
            for i,v in pairs(char:GetChildren()) do
                if v:IsA'Accoutrement' or v:IsA'Hat' then
                    game.Debris:AddItem(v, .3); 
                end
            end
        wait(5)
        canTrans =  true
         end
     end
end)


With UserInputService, you can do a lot, click on this link here And Also with ContextActionService, you can also do a lot, click on this link here If this worked, then please accept this answer If not please ask questions with no hesitation

4 comments

0

bad answer, he asked a fix for his function what you give is "KeyDown is deprecated" that doesn't not mean you cannot use it.

0

Yea I mean I know about UserInputService but I already know KeyDown and to me it feels simpler.

0

Also the script is inside StarterPack and When I tried your code in a localscript it did nothing just like my original script. Thats why I changed it to regular script!

0

InputBegan returns an InputObject not a key.

Maybe it's not the Mouse thing itself, but the variables you put before the mouse variable which are causing errors because the script is loaded before the objects.

at line 2, replace the char variable by

char = player.Character or player.CharacterAdded:wait()

at line 3, replace the hum variable by

hum = char:WaitForChild("Humanoid")

1 comment

0

Well now I run it in diabolical mode and it says "Players.Player1.Backpack.Script:8: attempt to index global 'mouse' (a nil value)" when i did GetMouse()

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