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

How to change the settings from touch to click on a vehicle spawner?

I made a car spawner but the tutorial taught me to make the car spawner button have to be stepped or walked over in order to respawn the car. I would like to change it so that it has to be clicked on. Thank you. The bottom of the script is the part that I need to know how to change. Also do I need to use the ClickDetector?

Here is my script:

model = script.Parent.Parent--Indicates that the script interacts with the model the button is grouped with. messageText = ""--If you want a message to appear upon pressing, type it here.

message = Instance.new("Message") message.Text = messageText backup = model:clone() enabled = true

function regenerate() message.Parent = game.Workspace model:remove()

wait(0)--Change this number to display the regen message as long as you want in seconds.

model = backup:clone()
model.Parent = game.Workspace
model:makeJoints()
message.Parent = nil

script.Disabled = true
script.Parent.BrickColor = BrickColor.new(26)--Black
wait(5)--Change this number to change the time in between regenerations via the button, in seconds..
script.Parent.BrickColor = BrickColor.new(104)--Purple
script.Disabled = false

end

function onHit(hit) if (hit.Parent:FindFirstChild("Humanoid") ~= nil) and enabled then regenerate() end end

script.Parent.Touched:connect(onHit)

1 answer

✓ Accepted answer

Put a ClickDetector in a Part, then you can use the MouseClick event of the ClickDetector.

workspace.Part.ClickDetector.MouseClick

The MouseClick event has one built-in parameter. It is equal to the Player object that clicked.

This differs from Touched. Touched will give you the Part that fired the event, while MouseClick gives you the Player that fired the event.

Connect it to a function, like normal.

clickdetector.MouseClick:connect(function(player)
	print( player.Name.." clicked!" )
end)

A nice thing about MouseClick is that there's no need to check if whatever fired the event contains a Humanoid or is a Player. This is because only Players can fire the event.


I would also recommend to just use a single function, instead of using one function to call another function. It's not needed in this case; you can just connect everything to the event.

2 comments

0

So do I remove the script I showed you in my description and replace it with the script that you say is better?

0

I didn't give you a script, I just told you how to use the event. You can modify the script that you have to work with a mouseclick event.

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