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

Why Won't This Script Clone "Fireball" Into PlayerScripts and Backpack?

Asked by 7 years ago

Whenever I run a server in Roblox studio it keeps saying that "Magic = nil" and I've tried renaming "Fireball", Renaming the Variable name, and Moving the PlayerScripts instead of Backpack.

repeat wait()
Player = game.Players.LocalPlayer
Button = script.Parent
Magic = game.ServerScriptService:FindFirstChild("Fireball")
until

Button.MouseButton1Down:connect(function(purchase)
    if 1+1==2 then
        local a = Magic:Clone()
        a.Parent = Player.PlayerScripts
    end
end)

3 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

I've noticed some grammar mistakes, lmao. Maybe some terminology errors too, I hope I helped though

repeat wait()
Player = game.Players.LocalPlayer
Button = script.Parent
Magic = game.ServerScriptService:FindFirstChild("Fireball")
until

Button.MouseButton1Down:connect(function(purchase)
    if 1+1==2 then
        local a = Magic:Clone()
        a.Parent = Player.PlayerScripts
    end
end)

This would not work at all. Before I go on, just a tip of advice:

if 1 + 1 == 2 then

is completely unnecessary...it doesn't do anything either.

Basically you can't do until for until a signal is connected to a function. Yeah, that didn't even make sense or sound right when I said it. Now I'll go on.

repeat wait() 

and until is pretty useless. You can just incorporate these values in the beginning of the script, like so:

local Player = game.Players.LocalPlayer
local Button = script.Parent
local Magic = game.ServerScriptService:FindFirstChild("Fireball")

I mean, there's no point of repeating this until a button is clicked. You could then have a BindableFunction in the players Backpack, so put one in StarterGui if you don't want to use a script to insert it.

local Func = Player.Backpack.Function

Now let's invoke Func when the signal is called (or whatever, I haven't been scripting for a while now, please excuse my vocabulary and terminology if it is incorrect)

Like this:

Button.MouseButton1Down:connect(function(purchase)
    --if 1+1==2 then
        local a = Magic:Clone()
        a.Parent = Player.PlayerScripts 
        Func:Invoke(true)
    -- end | Not needed one bit, if true then could work better, both aren't needed I believe 
end)

Now let's receive the Invoke with a simple function, and then do whatever you wanted after. I assume you wanted to do something after the button was clicked. You tried to make it repeat something (not needed) until the button was clicked, anyways:

I recommend making a "MainFunction" to do whatever you wanted to do after the buttons clicked. Example:

local function MainFunction()
        print(1 + 1)
end

local function isn't needed, don't you dare criticize me on that.

function Func.OnInvoke(whateveryouwanthereitcanbeusefulLater)
             MainFunction()
-- or any code here
end

Simple as that, but there are easier ways to do this like with a value. Now you may be saying what if they press the button more times, no they won't just delete the button or make a boolValue that must be true to OnInvoke and then turn it off after the press.

So all together:

local Player = game.Players.LocalPlayer
local Button = script.Parent
local Magic = game.ServerScriptService:FindFirstChild("Fireball")
local Func = Player.Function

local function MainFunction()
        print(1 + 1)
end

function Func.OnInvoke(whateveryouwanthereitcanbeusefulLater)
             MainFunction()
-- or any code here
end

Button.MouseButton1Down:connect(function(purchase)
    --if 1+1==2 then
        local a = Magic:Clone()
        a.Parent = Player.PlayerScripts 
        Func:Invoke(true)
    -- end | Not needed one bit, if true then could work better, both aren't needed I believe 
end)

I really hope that I helped, if I didn't I at least I hope I educated you on some other stuff in coding

Hope I helped, have a great day. Oh and if my vocabulary is off or if nothing makes sense I'm sorry feel free to make fun of me in comments :(

Ad
Log in to vote
0
Answered by
P100D 590 Moderation Voter
7 years ago
Edited 7 years ago

You seem to be using both local and server only script items.

game.Players.LocalPlayer is only accessible from the client ServerScriptService is only accessible from the server

Magic is probably nil because you're trying to access ServerScriptService from a local script.

If this is a server script, Magic is probably nil because "Fireball" doesn't exist in ServerScriptService at the beginning of the game. Try using :WaitForChild() instead of :FindFirstChild()

Also: Your repeat loop and if statement are both completely unnecessary.

Log in to vote
-2
Answered by 7 years ago
Edited 7 years ago

Forgive me if I give improper advice, I should believe that this will solve your problem, but some of my knowledge might be depreciated code or something that doesn't have proper usage.

I think it's because you named the variables inside your wait. Either that or it's because you didn't use local Magic = blah. If you just do

Player = game.Players.LocalPlayer
Button = script.Parent
Magic = game.ServerScriptService:FindFirstChild("Fireball")

repeat wait() until
    Button.MouseButton1Down:connect(function(purchase)
            if 1+1==2 then
                local a = Magic:Clone()
                a.Parent = Player.PlayerScripts
            end
    end)
end

If I'm wrong about this, then try doing the function outside of the wait, giving it a name, and run the function where the purchase is by doing functionName() like this:

Player = game.Players.LocalPlayer
Button = script.Parent
Magic = game.ServerScriptService:FindFirstChild("Fireball")

function functionName()
    Button.MouseButton1Down:connect(function(purchase)
            if 1+1==2 then
                local a = Magic:Clone()
                a.Parent = Player.PlayerScripts
            end
    end)
end
repeat wait() until
    functionName()
end

I don't know for entirely sure if functions work this way, but I do believe that they used to, and if that is the case, this should still work.

0
This is definitely wrong. P100D 590 — 7y
0
Do you even know how repeat until works? IT DOESNT REQUIRE AN END. "repeat code until stament" - NO END. RubenKan 3615 — 7y
0
oml TheeDeathCaster 2368 — 7y

Answer this question