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

How to make where if a value is true, onClick, it doesn't teleport you to that spot?

I have a basketball game I made, and if the Spot is taken (Which has a value for that), then you shouldn't go on it. Well, when it's taken and you press "Join Court" it puts you on the pad and only that pad. The "_PadValue" is the pad number, I didn't want to name the pads like Pad1 or Pad2, so I just used a value for the pad, and all the pads name's are _Pad. Please help?

script.Parent.Join.MouseButton1Click:connect(function()
	local player = game.Workspace:FindFirstChild(script.Parent.Parent.Parent.Name)
	local plr = script.Parent.Parent.Parent
	local court = script.Parent.Parent.Parent.Court.Value
	local courtfind = game.Workspace._Court1
	for i,v in pairs (courtfind._Team1:GetChildren()) do
	local pad = tostring(v)
	if v.Values._PadValue.Value==1 and v.Values._PadTaken.Value==false then
	player.Torso.CFrame=CFrame.new(v.tpspot.Position)
	script.Parent:Remove()
	end
	if v.Values._PadValue.Value==2 and v.Values._PadTaken.Value==false then
	player.Torso.CFrame=CFrame.new(v.tpspot.Position)
	script.Parent:Remove()
	end
	end
end)

1 answer

✓ Accepted answer

##Your Problem

Your code is a bit sloppy, but you only have 1 real error. This would be your second if statement, on line 12.

What's happening is the script is reading over the first if statement, and if it passes then it will teleport you. BUT - the second if statement is always being read last and henceforth you will always be teleported to the second _PadValue.

##How to fix

You can fix this by replacing the second if statement with an elseif statement. This will make it so that the second condition will only be checked if the first one has not passed.

###Notes

  • 'elseif' statements do not require a second 'end' statement.

  • Define variables that are static on the outside of your event. This is more efficient because then they are not being defined everytime the event is called.

  • Gui buttons run on the client, so you can always index LocalPlayer from game.Players to retrieve the client. But this can only be done in a LocalScript. I don't want to break your code so you can implement this later if you'd like.

  • Tab your code correctly, it keeps it clean and legible.

  • Remove is deprecated, use Destroy :)

####Code

local player = game.Workspace:FindFirstChild(script.Parent.Parent.Parent.Name)
local court = script.Parent.Parent.Parent.Court.Value
local courtfind = game.Workspace._Court1

script.Parent.Join.MouseButton1Click:connect(function()
	for i,v in pairs (courtfind._Team1:GetChildren()) do
		--Put some extra variables cause you had repeated lines of code
		local pad = tostring(v)
		local padVal = v.Values._PadValue.Value
		local padTaken = v.Values._PadTaken.Value
		local tor = player.Torso
		--Check this first so you don't need to have any 'and' statements
		if not padTaken then
			if padVal == 1 then
				tor.CFrame = CFrame.new(v.tpspot.Position)
		 	elseif padVal == 2 then
				tor.CFrame = CFrame.new(v.tpspot.Position)
			end
			--Seems like you do this everytime. So put it on the outside
			script.Parent:Destroy()
		end
	end
end)

2 comments

0

Thanks! it worked, now we can continue our game thanks to you, im sorry it's sloppy that is just how I lay my scripts out. Thanks bro!

0

No problem! Glad I could help :D

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