✓ Accepted answer
You cannot set CFrames of instances using strings, you must use Vector or CFrame values.
Also, I'm not sure if this is in a local script or a server script because the code is entirely wrong, if you're using a server script you cannot index the player in a server script using game.Players.LocalPlayer, but if your using a local script you can, but you cannot change a CFrame in a local script in FilteringEnabled because it won't replicate to the server.
I would suggest just keeping a table of Vector3 or CFrame values (I prefer CFrame) and set the CFrame or Position to that value in the table.
local Tab = {CFrame.new(26, 0.5, -1); CFrame.new(26, 0.5, -33)}
local locationselected = Tab[math.random(1, #Tab)]
while wait(5) do
head.CFrame = locationselected
end
Alternatively, you can also use the new Random.new() function which is much more efficient and has more uses.
local Tab = {CFrame.new(1,1,1); CFrame.new(2,2,2)}
local rng = Random.new()
local function returnCFrame()
local result = rng:NextInteger(1, #Tab)
return Tab[result]
end
while wait(5) do
local partCFrame = returnCFrame()
head.CFrame = partCFrame-
end
If you have any more questions don't hesitate to ask :D
9 comments
Alright! Ill test it out. I'm trying to make it server-script compatible, so will this work in ServerScriptService?
And how would I go about telling the script to find the players name, If i can't reference the game.Players.LocalPlayer.Name?
Yes, but you need to access the player other than using 'game.Players.LocalPlayer', there are several methods such as using PlayerAdded and using the parameter 'Player' and then doing local char = player.Character:WaitForChild('Head') and then setting the CFrame there.
Alright! Well I was just messing with it and it just decided to work for some reason. Here is what I got:
local Tab = {CFrame.new(26, 0.5, -33); CFrame.new(26, 0.5, -1)} local locationselected = Tab[math.random(1, #Tab)] while true do wait(5) game.Workspace:WaitForChild(game.Players.LocalPlayer.Name).Head.CFrame = locationselected wait() end
It's only going to work in studio, once you test it in a real server you will realize it's not going to work. Try using this https://wiki.roblox.com/index.php?title=API:Class/Players/PlayerAdded
Ugh nevermind, It only teleports me to one. Is there a way I can make this loop over and over again? While true do is making it so it will keep choosing the same one
It's only going to teleport to one because your randomly choosing the location once, if you want it to choose a new location every loop then add the code that randomly chooses the CFrame inside the loop so it chooses a new one.
Alright... But if I add a playerAdded event, will the teleport be in sync for all players? Or will it reset for the LocalPlayer when they join?