Why is this script not working

i tried to make some part explode if the player clicks on the e button on their keyboard but it didn’t work, it’s on localscript, i also tried with a server script but also didn’t work, i also removed the if gameprocessedevent == true but it still didn’t work

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent == true then
		if input.KeyCode == Enum.KeyCode.E then
			Instance.new("Explosion", workspace.Part)
		end
	end
end)

I just tested the following code in an empty baseplate and it worked fine:

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)

		if input.KeyCode == Enum.KeyCode.E then
			Instance.new("Explosion", workspace.Part)
		end

end)
1 Like

You might be creating the explosion but not seeing it because it’s by default spawned at 0,0,0. After creating it, set it’s position to the part’s position like this

local explosion = Instance.new("Explosion", workspace.Part)
explosion .position = workspace.Part.Position

Also, you should be checking if gameProcessedEvent is False, not True.

i tried it out but it still didn’t work, the problem is that it won’t add an explosion instance to the part at all

Is this in a client script or a server script?

the script itself is a client script

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then -- you want this to be false, not true
		if input.KeyCode == Enum.KeyCode.E then
			local explosion = Instance.new("Explosion") -- we'll need to do a few things
			explosion.Position = workspace.Part.Position
			explosion.Parent = workspace.Part
		end
	end
end)

Something like this should work in a client script

use this script:

local uis = game:GetService("UserInputService")
uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		wait(0.1)
		print("explosion")
		local ex = Instance.new("Explosion")
		ex.Parent = game.Workspace.Part
		ex.Position = game.Workspace.Part.Position
		
	
			
		end
	
end)

it still didn’t work even when i pressed the “e” key

It’s either still a server script, or you didn’t place the script in a correct container.

Ekrano nuotrauka 2022-10-08 213931

no, try the new inputservice!!

The script has to be placed in the StarterPlayerScripts folder.

1 Like

Exactly this, LocalScripts cannot run in Workspace.

1 Like