I don't know what's wrong with my randomizer script

I made a script that teleports a player to the map if the “SpawnInMap” bool value equals true. If not teleports to a lobby. The only line that seems to move the player is “character:MoveTo(workspace.CharSpawnSelect.SpawnLocation.Position)” Else doesn’t work. The randomizer doesn’t seems to be randomizing. Here’s screenshots:

Please help me…

1 Like

Hi, I think this line might be the issue

EDIT: WHoops, posted before finishing >.<’ one moment please

if Value == true then ...

Since you set Value to script.Parent.Value, Value is almost certainly going to hold a reference to an Instance. It’s probably a BoolValue guessing from your script, but you can’t just compare a BoolValue object to an actual boolean value. Instead you have to get the value that that BoolValue object holds, with the BoolValue.Value property. Otherwise the condition will never evaluate to true, same with the elseif condition, so neither clause ever runs.

So try

if Value.Value == true then ... elseif Value.Value == false then

Oh, and you should probably rename the Value variable to something more descriptive, because it gets really confusing to talk about when talking about the variable Value, the Instance named Value and the Property of that Instance named Value xD

BTW in future please copy/paste your code instead of using a screen shot, it’s more convenient for the people replying :slight_smile:

Also, if you feel like it the debugger is a very handy tool for this kind of bug. You can step through code 1 line at a time, and you’d have seen that the code never runs either the if or the elseif clause, which would hint you to the solution.

Also also, for this exact reason it’s best practice to not have an elseif in this case but an else, precisely because there’s a finite number of case all of which you want to check (either it’s true or false), so you can avoid this bug more easily that way.

Also also also, you don’t need to compare a boolean value to true to figure out if it’s true, you can just use the boolean value itself. E.g. if Value.Value then ....

Hope this helps!

1 Like

I just noticed that the script that changes the value isn’t changing it


Screen Shot 2022-11-10 at 2.05.56 PM

1 Like

Thanks! I’ll try it right now. I’ll reply if it does work or not.

2 Likes

To add a little bit more readability and compactness to the code (in the died event), you can do this instead:

--[[
this if statement will return
"false" if "Value" is not true
"true" if "Value" is not false
]]
if Value then
    -- do stuff
else
    -- do stuff
end
1 Like

As it seems. I think i need to wait it. Because without the wait it teleports to the selected spawn even though the player is dead. But when i do a wait. It breaks the teleport part of the script (i think)
Here’s the wait line i did

wait(game:GetService(“Players”).RespawnTime + 0.1)

2 Likes

You’re right, but waiting for a set amount of time might not be reliable. If it works it’s fine, but I’d prefer to explicitly do something when the new character spawns in instead. You can use the player.CharacterAdded event for that.E.g.

player.CharacterAdded:Connect(function(character)
    character:PivotTo(chosenSpawn.CFrame) --MoveTo is fine also
end)

… although unfortunately the default spawn system isn’t that well made, so you might have to wait a frame or two before moving the character anyway :stuck_out_tongue:

2 Likes

Just curious is this happening in player.PlayerScripts also?

Yes. I probably should put it in StarterGui.

It isn’t changing because the variable SpawnInMap because it does not hold a reference (thanks to Lua). Instead, it will make a copy of the value you assigned to it. Take for example the following snippet of code:

local x = 10
local y = x
y = 20

if x == 10 then
  print('value has not changed') -- will go this route
else
  print('value has changed')
end

Despite setting y to x, setting it to a different value won’t change x. Instead, we need to do it directly (x = 20).

So, for your code to work, you must set player.PlayerScripts.SpawnInMap.Value to true, and not a variable that simply stored of a copy of it.

2 Likes

Now it seems it doesn’t teleport the player to the randomized spawn.

is script.Parent.Value changed by client or server? It’s hard to tell with those icons.

It is changed by another local script (client)

Then that’s why it is not working. You need to change the value on the server instead.

1 Like

Ok. I’ll try that later. I’ll let you know if it fixes it.

How is the value changed anyway, may i ask?

i’ma try it rn. but roblox studio is updating lol. so it might take a bit

It’s now changing the variable. And checking the variable. But it doesn’t teleport the player to the randomly selected spawn

Guys! I fixed the problem. I made sure that the variable changes. And fixed the teleporting. For the teleporting i had to do it on the Server Side. Thanks for helping me y’all! :happy1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.