Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Question about string values and math.random?

Asked by 5 years ago

So Im trying to make it, so my game waits 5 seconds, then teleports the player to one of two bricks. This is the script i have:

locations = {"26, 0.5, -1", "26, 0.5, -33"}
locationselected = locations [math.random(1, #locations)]
while true do
    wait(5)
    game.Workspace:WaitForChild(game.Players.LocalPlayer.Name).Head.CFrame = CFrame.new(locationselected)
end

But it's saying "Vector3 Expected, got string"

Any help?

1 answer

Log in to vote
1
Answered by 5 years ago

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 -- sample code
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

0
Alright! Ill test it out. I'm trying to make it server-script compatible, so will this work in ServerScriptService? SBlankthorn 329 — 5y
0
And how would I go about telling the script to find the players name, If i can't reference the game.Players.LocalPlayer.Name? SBlankthorn 329 — 5y
0
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. YabaDabaD0O 505 — 5y
0
Alright! Well I was just messing with it and it just decided to work for some reason. Here is what I got: SBlankthorn 329 — 5y
View all comments (5 more)
0
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 SBlankthorn 329 — 5y
0
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 YabaDabaD0O 505 — 5y
0
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 SBlankthorn 329 — 5y
0
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. YabaDabaD0O 505 — 5y
0
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? SBlankthorn 329 — 5y
Ad

Answer this question