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

From "Touched:Connect(function(hit)" to GUI click?

Im editing an script that works for a touch brick function

script.Parent.Union.Enter.Touched:Connect(function(hit)
	wait()
	local plr = nil
	if (GlobalCooldown) then return end
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		Open(plr)
		GlobalCooldown = true
		wait(GlobalCooldownAmt)
		GlobalCooldown = false
		repeat wait(0.1) until plr:DistanceFromCharacter(script.Parent.Union.Position) > 15
	end
end)

But i want it to work if you click a gui button, im doing this but dont work, please help

script.Parent.MouseButton1Up:Connect(function(clic)
	wait()
	local plr = nil
	if (GlobalCooldown) then return end
	if game.Players:GetPlayerFromCharacter(clic.Parent) then
		plr = game.Players:GetPlayerFromCharacter(clic.Parent)
		Open(plr)
		GlobalCooldown = true
		wait(GlobalCooldownAmt)
		GlobalCooldown = false
		repeat wait(0.1) until plr:DistanceFromCharacter(clic.Parent)
	end
end)

1 answer

✓ Accepted answer

Unlike .Touched, .MouseButton1Click does not give you any arguments. As guis can only be manipulated through LocalScripts, you should be able to just get the LocalPlayer itself. LocalScripts only execute inside of something that is individual to each player, such as a gui, so the LocalScript should be inside your gui button (which it looks like it is), and then you can do something like this:

script.Parent.MouseButton1Up:Connect(function()
	local plr = game.Players.LocalPlayer
	--do stuff
end)

Each player has their own set of guis, so the LocalPlayer will be the player that owns the gui that was clicked (aka the player who clicked the gui).

Hope this helps!

3 comments

0

I changed what u said and still not working

0

Any errors? If not, try adding prints at each step and see where it goes wrong.

0

Nvm, I was doing something bad, thank you very much for the help, now it worked

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