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

Can you make an too with an script only run when the player has the tool?

Asked by 6 years ago
Edited 6 years ago

So I have a tool that gives the player an keystroke class , and i had a problem with the gui that it wont appear after the player respawns, i have solved that problem but now all the scripts run without me even choosing a class(which would give the player a tool WHICH would load the script. the script that gives the player the tool(LocalScript)

function onClicked()
 script.Parent.Parent.Parent:Destroy()
end
script.Parent.MouseButton1Click:connect(onClicked)

local p = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
    local tool = script.Parent.yukio:Clone()
    tool.Parent = p.Backpack
end)

AND the script that loads in the gui ( Menu is the name of the gui)

local Menu = script.Menu

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        local MenuClone = Menu:Clone()
        MenuClone.Parent = player.PlayerGui
    end)
end)

I had an old script for the gui which was working perfectly,only that it wouldnt appear after the player respawned:

    Menu = script.Menu


game.Players.PlayerAdded:connect(function(player)
    local MenuClone = Menu:Clone()
    MenuClone.Parent = player.PlayerGui
end)

I have also seen that when i removed player.CharacterAdded:Connect(function()... end) the scripts and gui would all work again but still wont appear after the player respawned

0
just put the menu in startergui. LukeGabrieI 73 — 6y
0
Tried it, still dosent work :( tonicos33 6 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

for one you should probably combine the two mouse click functions:

local p = game.Players.LocalPlayer

function onClicked()
    local tool = script.Parent.yukio:Clone()
    tool.Parent = p.Backpack
    script.Parent.Parent.Parent:Destroy()
end
script.Parent.MouseButton1Click:connect(onClicked)

it would be easier and simpler if you left the menu inside of playergui so you don't need the server script.

There are two different things I can think of what you are trying to do. 1. disable the script when the player is not holding the tool and enable it when they are 2. only run code in the script when the player is holding the tool

the difference? 1 restarts the script from the beginning while 2 keeps it running.

To do 1 just put a script inside of the tool with a function that triggers when the tool is equipped that sets the scripts' Disabled properties = false and another function for when the tool is unequipped that sets it to true.

For 2 just add

if player.Character:FindFirstChild("yukio") and player.Character.yukio.Equipped then

end

around the code you don't want run if the player isn't holding the tool.

0
Thanks, i was going for 2, as you said but the thing about puting the gui in the startergui... the thing is its a gui that covers the whole screen, an char select screen tonicos33 6 — 6y
Ad

Answer this question