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

teleportation local script textbutton does not work! [FIXED] THANK YOU?

Hello So i am trying to make this script work for maybe already 2 days i asked others help they tried to help me but it still does not work in client roblox launcher but it does in studio Help me please?

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
local spawns = workspace:WaitForChild("Map"):FindFirstChild("Spawns")
frame = script.Parent.Parent.Parent

  

function Click()
Camera.CameraType = Enum.CameraType.Custom
frame.Visible = false
local Spawns = spawns:GetChildren()
local Selected = Spawns[math.random(1,#Spawns)]
Character:MoveTo(Selected.Position + Vector3.new(0, 5, 0))
end

script.Parent.MouseButton1Down:Connect(Click)

script location https://imgur.com/a/8OHjwWP error https://imgur.com/a/RVNnaUf

2 comments

0

before u made the code a code block get ur code out, then mark ur text and put it in a code bock, that way its readable

0

you shouldn't have 2 variables be so similarly named like "spawns" and "Spawns". name them appropriately so that you don't accidentally use the wrong variable. maybe "spawnsModel" and "spawnParts"?

3 answers

✓ Accepted answer

Seeing how things arent going great for you, and that Twinbrotato tried to help you, but you still result in an error, and that is the case of math.random. You need to make sure all the spawns have loaded before you run your math.random function. To do so, you simply check with a if statement, and while not always recommended, in your case, i would recommend you use a repeat loop. Heres how that if statement should run.

--Variables you have already defined.
local spawns = workspace:WaitForChild("Map"):WaitForChild("Spawns")
local Spawns= spawns:GetChildren()

--Check if spawns have loaded.
if #Spawns<1 then
	--Recommended, ONLY if you know there are spawns in the model, otherwise place a retry limit so your script doesnt loop here forever(and also place a failsafe).
	repeat
		wait()
	until #Spawns>=1
end
--Now we can get a random part.
local Selected= Spawns[math.random(1,#Spawns)];
--Rest of your code.

Another thing, if you forgot to anchor those spawns, i would do so, they could be falling off the map(or through) and then getting destroyed from the void.

4 comments

0

That repeat loop wouldn't work as the value of Spawns isnt being updated. You'd have to redefine it in the repeat loop. And yea, them being unanchored is most likely the issue. Experienced it far too many times lol

0

Yea, forgot about that.

0

had to change some things and now it works perfectly thank you everyone for helping winky face

0

np.

attempt to index upvalue 'spawns' (a nil value)

This means that you are trying to do something with the variable spawns when it's nil.

You defined spawn as such:

local spawns = workspace:WaitForChild("Map"):FindFirstChild("Spawns")

This means that a child named "Spawns" does not exist in workspace.Map, either because it has not loaded in yet, or it really doesn't exist because you haven't added it in or misspelled its name.

Once you've double checked that it does exist in workspace.Map and that the name is correct, try using WaitForChild instead of FindFirstChild as "Spawns" might not have loaded in yet.

3 comments

0

so i did something and fixed that error now i get this one https://imgur.com/a/iXdg1Fl

0

bad argument #2 to 'random' (interval is empty) that means that in your math.random, the second argument is lesser than the first. that means that #Spawns is 0 meaning your spawns model has nothing in it. also don't forget to mark my post as the answer if it solved your issue

0

wdym there nothing here https://imgur.com/a/VCzWXnI

Hey popgoesme700 so i tried to make your script work when i click an button i made it tried in studio and in roblox launcher it did not gave me any errors just the problem is that it does not spawn in roblox player launcher but it does in studio how can i fix this already?

--Variables you have already defined.
local spawns = workspace:WaitForChild("Map"):WaitForChild("Spawns")
local Spawns= spawns:GetChildren()
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera

if #Spawns<1 then
repeat
wait()
until #Spawns>=1
end

  

function Click()
local Selected= Spawns[math.random(1,#Spawns)];    Character:MoveTo(Selected.Position + Vector3.new(0, 5, 0))
print(Selected)
end

  

  

script.Parent.MouseButton1Down:Connect(Click)

1 comment

0

lol for some reason i smashed keyboard and now when i saved and played in roblox player it works lol lmao

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