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

Click textbutton to add the tool, click same textbutton to remove is not funtioning?

Asked by 4 years ago

I plan to create a screen GUI in the starter GUI that has a script, textbutton, and frame (the textbutton and frame being grouped together) that can add the tool to your inventory when you click on the button, and it is removed when you click again on the same textbutton. When I test the game, it doesn't do anything (AKA, it just sits there being able to be clicked, but the script wasn't functioning). I can tell that there are mistakes, but where are they, and how can I fix them? Here's the script:

local plr = game.Players.LocalPlayer
local tools = plr.Backpack
local Tool = game.Lighting.Sword
Player = script.Parent.Parent.Parent.Parent


function Remove()


     if tools:FindFirstChild("Sword") then


        tools.Tool:Destroy()
        wait()
    end 
end

function Add()

    if game.Lighting:FindFirstChild("Sword") then


        Tool = game.Lighting.Tool:clone()
        Tool.Parent=Player.Backpack
        wait()
    end
end

script.Parent.MouseButton1Down:connect(function()
        if Player.Backpack:FindFirstChild("Sword") then
        Remove()
else
        Add()

    end
end)

If you couldn't understand the way I set my screen GUI up, here's the link to the game:

https://www.roblox.com/games/4390322172/Scripting-help-demo-for-mistake

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Just a side note: Lighting is not intended for storage. We have ServerStorage and ReplicatedStorage to store things. So why use Lighting? It makes no sense.

You need remotes.

This is all being done on the client, therefore you need remotes. Specifically, remote events.

A typical place for remotes is ReplicatedStorage, since the server and client can access them. Name it whatever you want, but have a variable that points to it for easy access.

remote_event.OnServerEvent:Connect(function(client)

end)

client points to the player that fired the remote event. From here you can do the same checks you did before.

if not client.Backpack:FindFirstChild("Sword") then
    game:GetService("ServerStorage").Sword:Clone().Parent = client.Backpack
end

And on the client side, get rid of Remove, Add, Player, Tool, and tools.

local remote_event = ... -- you know what to do

-- Think of your mobile players and console players, don't use Mouse event
script.Parent.Activated:Connect(function()
    remote_event:FireServer() -- no player argument passed - roblox does this implicitly
end)
0
Thank you for the approach. I will see what I can do with it. BabyBearPowStuff 11 — 4y
Ad

Answer this question