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

When I press the key "F" the part should get destroyed, what is wrong?

I need help with this script

This is the error "Workspace.Crystal.Script:5: attempt to index a nil value"

script.Parent.ClickDetector.MouseHoverEnter:Connect(function(player)
	
db = true

player:GetMouse().KeyDown:connect(function(k)
	k = k:lower()
	if k == "f" then
		if db == true then
			script.Parent:Destroy()
		end
	end
end)
end)

What is wrong here?

It supposed to when you hover your mouse over the part and then press "F" it should get destroyed.

Thanks

11 comments

0

you're connecting a keyboard event to the mouse, that's the problem

0

But if I want to make a script that when I have my mouse on a part and then press "F" it gets destoyed. How do I do that?

0

embed a UserInputService or ContextActionService function inside of the ClickDetector function

0

KeyDown in general is deprecated

0

you gotta use UserInputService

0

That's why it isn't working

0

Okay thank you, I'll try that.

0

Np, did my answer help?

0

I'm working on it, but I think it will work.

0

Use :Connect not :connect

0

ok

1 answer

✓ Accepted answer

KeyDown is deprecated, use the UserInputService.

You'll be using it like this.

script.Parent.ClickDetector.MouseHoverEnter:Connect(function(player)

	db = true
	
	game:GetService("UserInputService").InputBegan:Connect(function(k)

		if k.KeyCode == Enum.KeyCode.F then
			if db == true
				script.Parent:Destroy()
			end
		end

	end)
end)

1 comment

0

ty

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