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

How to pick a random magic from a table?[Not Sloved]

Asked by 9 years ago

Hello there im having problems picking a random magic from a table using math.random. Youtube wont help me so i want to ask u guys for help.

function OnPlayerAdded(Player)
Player:WaitForDataReady()
Player = game.Players.LocalPlayer
    Magic = Instance.new("StringValue", Player)
    Magic.Name = "Magic"
    if Player:LoadString("Magic") ~= "" then
        Magic.Value = Player:LoadString("Magic")
    else
        Elements = {"Water", "Fire", "Earth"}
        i = math.random(1, #Elements)
        Player.Magic.Value = Elements[i]
        Player:SaveString("Magic" , Player.Magic.Value)
        M = Instance.new("Message", workspace)
        M.Text = Magic.Value
        game.Debris:AddItem(M, 5)
    end
    if Player.Magic.Value == "Fire" then
        x = game.Lighting.Fireball:clone()
    elseif Player.Magic.Value == "Water" then
        x = game.Lighting.Waterball:clone()
    elseif Player.Magic.Value == "Earth" then
        x = game.Lighting.Rockball:clone()
    end
    x.Parent = Player.Backpack
end

game.Players.PlayerAdded:connect(OnPlayerAdded)






2
Have you even attempted at making the script? And there is this thing called wiki.roblox.com I suggest looking on it. Ryzox 220 — 9y
0
Yes i ready have here it is: KFCPhoeyu 41 — 9y
0
If I remember correctly, WaitForDataReady was broken. If you take that out, your script should work. Spongocardo 1991 — 9y
0
Still wont work :( KFCPhoeyu 41 — 9y
View all comments (2 more)
0
Why are you changing Player to the LocalPlayer when you've already got the player? What type of script are you using? Spongocardo 1991 — 9y
0
A script normal one KFCPhoeyu 41 — 9y

1 answer

Log in to vote
-1
Answered by 9 years ago

Assuming you have this as a Script, you don't need the third line as you can't access the LocalPlayer from a server script. Plus, you wouldn't need to use the LocalPlayer anyway as you're already getting the player from the PlayerAdded event.

Another reason you shouldn't use a LocalScript for you code is because LocalScripts can't handle data persistence saving and loading as it is server sided. I would also make your variables in the function local as variables can conflict if a new player enters.

Also, I believe WaitForDataReady is broken, so I took it out of your script.

The final script is below:

Elements = {"Water", "Fire", "Earth"} --Have Elements outside the script for ease.

function OnPlayerAdded(Player)
    local Magic = Instance.new("StringValue", Player)
    Magic.Name = "Magic"
    if Player:LoadString("Magic") ~= "" then
        Magic.Value = Player:LoadString("Magic")
    else
        local elem = math.random(1, #Elements) --I recommend you use local variables so that variables don't conflict.
        Magic.Value = Elements[elem]
        Player:SaveString("Magic" , Magic.Value)
        M = Instance.new("Message", Player.PlayerGui) --Put message in PlayerGui so that only the player can see it.
        M.Text = Magic.Value
        game.Debris:AddItem(M, 5)
    end
    if Magic.Value == "Fire" then
        local x = game.Lighting.Fireball:Clone()
    x.Parent = Player.Backpack
    elseif Magic.Value == "Water" then
       local  x = game.Lighting.Waterball:Clone()
    x.Parent = Player.Backpack
    elseif Magic.Value == "Earth" then
        local x = game.Lighting.Rockball:Clone()
    x.Parent = Player.Backpack
    end
end

game.Players.PlayerAdded:connect(OnPlayerAdded)

I would suggest using DataStores for your data saving/loading as Data Persistence is quite old now.

I hope my answer helps you. If it did, be sure to accept it.

0
Why the -1 rep? Just wondering. Spongocardo 1991 — 9y
0
Not working :/ KFCPhoeyu 41 — 9y
0
Any errors? Spongocardo 1991 — 9y
0
19:07:14.003 - Data for player not yet loaded, wait for DataReady KFCPhoeyu 41 — 9y
0
Try adding WaitForDataReady back in my script Spongocardo 1991 — 9y
Ad

Answer this question