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

How do I fix this tool-giving script, which only works in studio, it doesn't work in game?

Asked by 6 years ago

Hi. Was testing a Gui vending machine script today. It however, only works in studio. When I try to test it in ROBLOX, the TextButton won't give the tool. Here is the script:

tool = game.ServerStorage.Aquafina

script.Parent.MouseButton1Down:connect(function()
    local thetool = game.Players.LocalPlayer.Backpack:FindFirstChild("Aquafina")
    if thetool then
            print "ToolGiven"

    else
    local g = tool:Clone()
    g.Parent = game.Players.LocalPlayer.Backpack

end
end)

Best Regards, Boomer

0
How would the server know what "LocalPlayer" is? hiimgoodpack 2009 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

This is because the client/player doesn't have access to the "ServerStorage" service, meaning your code won't work.

Please do this server-sided.

Here is the code on how to give a tool to a player, server-sided. You need to use Remote Events/Functions to do this


local ServerStorage = game:GetService("ServerStorage"); local Tool = ServerStorage:FindFirstChild("Aquafina") --Function that's being run when certain event has happened.. --Assuming Player is defined if Player.Backpack:FindFirstChild("Aquafina") ~= nil then print("Already given the tool to this player") else local cloneTool = Tool:Clone() cloneTool.Parent = Player.Backpack end

That's just one way to do it, please note there's no fail-safety

Ad

Answer this question