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

Random Tool Giver Script does not work. I have spent an hour trying to fix it. Please help me?

Asked by 3 years ago
Edited 3 years ago

Why does my random tool giver script not work? Please help me.

place = game.ServerStorage
local player = game.Players.LocalPlayer

tools = {}
ls = {}
function boop(Player)
    repeat wait() until player.Backpack

    for i,v in pairs(place:GetChildren()) do
        print(v.Name)
        if v:IsA("Tool") then
            table.insert(tools, #tools + 1, v.Name)
        elseif
           v:IsA("LocalScript") then
            table.insert(ls, #ls + 1, v.Name)
        end
    end
if #tools > 0 then
    local randomtool = math.random(1, #tools)
    local chosentool = tools[randomtool]
    local tool = place.AK47M:Clone()
    local tool2 = place.AKS74U:Clone()

    tool.Parent = player.Backpack
    tool2.Parent = player.Backpack
end
if #ls > 0 then
    local randomls = math.random(1, #ls)
    local thels = ls[randomls]
    local ls1 = place[thels]:Clone()
    local chosenls2 = place[thels]:Clone()

    ls1.Parent = player.Backpack
    chosenls2.Parent = player.StarterGear
end
end
script.Parent.ClickDetector.MouseClick:Connect(boop)

3 answers

Log in to vote
0
Answered by
panvvan 20
3 years ago

u used server storage which for anti exploiting reasons cant be accessed from a local script or the client u will need to put everything in the Replicated Storage Folder

0
I changed the top line EricStoynov1749 -5 — 3y
0
I changed the top line to this place = game.ReplicatedStorage. I have moved the tools and it still does not work. EricStoynov1749 -5 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

First of all, Pannvvan is correct, a local script or client is unable to access the server storage. However, it is able to access Replicated Storage.

At lines (19 - 25):

19      local randomtool = math.random(1, #tools)
20      local chosentool = tools[randomtool]
21      local tool = place.AK47M:Clone()
22      local tool2 = place.AKS74U:Clone()
23   
24      tool.Parent = player.Backpack
25      tool2.Parent = player.Backpack

The issue is right in lines 21 and 22. You look for tools by the names AK47M and AKS74U. And give the player these tools at lines 24 and 25. However, you give the players tools that were not selected randomly because you preset the tool values at lines 24 and 25.

It looks like you were on the right track at lines 19 and 20. However, you end up giving the players tools that are not random. (Or do not exist!)

I made the fixes in the code block below:

place = game.ReplicatedStorage
local player = game.Players.LocalPlayer

tools = {}
ls = {}
function boop(Player)
    repeat wait() until player.Backpack

    for i,v in pairs(place:GetChildren()) do
        print(v.Name)
        if v:IsA("Tool") then
        table.insert(tools, #tools + 1, v)
        --The table was getting the names of the tools not the objects.
        elseif
            v:IsA("LocalScript") then
            table.insert(ls, #ls + 1, v.Name)
        end
    end
    if #tools > 0 then
        local randomtool = tools[math.random(1, #tools)]
        local randomtoolClone = randomtool:Clone()
        --You were giving preset tools to the player, not random ones.

        randomtoolClone.Parent = player.Backpack
    end
    if #ls > 0 then
        local randomls = math.random(1, #ls)
        local thels = ls[randomls]
        local ls1 = place[thels]:Clone()
        local chosenls2 = place[thels]:Clone()

        ls1.Parent = player.Backpack
        chosenls2.Parent = player.StarterGear
    end
end

workspace.Part.ClickDetector.MouseClick:Connect(boop)

I hope this helps!

Things to consider:

Make the place variable a local variable.

Copy this script onto a server script. (Normal.) And send a RemoteEvent signal to the Server whenever you want to give the player items to prevent exploitation.

I would recommend putting a folder inside Replicated Storage just for your tools and making an appropriate name for it! It can increase efficiency if you have many objects in Replicated Storage by making the script loop through fewer instances.

If you do this make sure you change the place variable.

0
Here is the script rn. I get this error. 20:57:50.717 - Workspace.Part.Script:7: attempt to index nil with 'Backpack' EricStoynov1749 -5 — 3y
Log in to vote
0
Answered by 3 years ago

I re-made the script myself. Thanks for the help.....

Answer this question