So I wanted a Textbutton that is a server sided script inside that will give the clicked player a tool inside of “Lighting”, and the tool name is based on the button name of the button pressed.
For now, my script is like this:
script.Parent.MouseButton1Click:Connect(function()
local username = script.Parent.Parent.TextLabel
print(script.Parent.Parent.TextLabel.Text)
local tool = game.Lighting:FindFirstChild(script.Parent.Name)
local clone = tool:Clone()
local plr2 = game:GetService("Players")
local plr = plr2:FindFirstChild(username.Text)
clone.Parent = plr.Backpack
end)
In Line 2, the TextLabel, there’s a LOCALSCRIPT inside of it and however it did not set the text as the username:
local player = game:GetService("Players").LocalPlayer
script.Parent.Text = player.Name
Yep, so thats why you have to use a remote event. Make it look like this:
Step 1. Create a remoteEvent in ReplicatedStorage called “ToolEquipRemote”
Step 2. Create a serve sided script in serverScriptService
Step 3. Change the local script’s code to this:
script.Parent.MouseButton1Click:Connect(function()
local username = script.Parent.Parent.TextLabel.Text
game:GetService("ReplicatedStorage").ToolEquipRemote:FireServer(username)
end)
Step 4. Create the text in the server script to this:
game:GetService("ReplicatedStorage"):WaitForChild("ToolEquipRemote").OnServerEvent:Connect(function(plr, targetPlrName)
local tool = game:GetService("Lighting"):FindFirstChildOfClass("Tool"):Clone()
tool.Parent = game:GetService("Players")[targetPlrName].Backpack
end)
You need to have a local script inside your starter pack, a remote event in replicated storage and the server script in the serverscriptservice.
This script should be in the local script:
local RemoteEvent = game:GetService('ReplicatedStorage").RemoteEvent
script.Parent.MouseButton1Click:Connect(function()
local username = script.Parent.Parent.TextLabel
RemoteEvent:FireServer()
end)
ServerScript:
local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
RemoteEvent.OnServerEvent:Connect(function()
print(script.Parent.Parent.TextLabel.Text)
local tool = game.Lighting:FindFirstChild(script.Parent.Name)
local clone = tool:Clone()
clone.Parent = plr.Backpack
end)
Change the name of the remote event to whatever you want
-- local script
local remote = game:GetService("ReplicatedStorage").RemoteEvent -- the remote event
local button = script.Parent -- the textbutton
button.MouseButton1Click:Connect(function()
local username = -- path to username text here
remote:FireServer(username) -- send request to the server
end)
-- server script
local remote = game:GetService("ReplicatedStorage").RemoteEvent -- the remote event
remote.OnServerEvent:Connect(function(plr, username)
-- "plr" is automatically passed: its the player who fired the remote
-- "username" is the argument that was passed through the remote
-- your code here
end)
Couple of issues within your statement, you need to first realize that you may only use the MouseButton1Click function of a TextButton, inside of a Local Script. Now for what you’re trying to accomplish here is pretty simple, all you’d need is a TextButton, a Local Script, a Remote Event and a normal Script in ServerScriptService.
The following code is what I’d assume is you’d need:
Local Script:
local Button = script.Parent
local EquipRemote = game.ReplicatedStorage:WaitForChild("EquipRemote") --Or whichever folder you may want to keep it in :D
Button.MouseButton1Click:Connect(function()
EquipRemote:FireServer()
end)
Server Script:
local EquipRemote = game.ReplicatedStorage:WaitForChild("EquipRemote")
EquipRemote.OnServerEvent:Connect(function(Player) --Player is automatically passed as a parameter when using Remotes
local Tool = game.Lighting:FindFirstChild("NameOfTool"):Clone()
Tool.Parent = Player.Backpack
end)
It’s as simple as that, now you may have to adjust the code to your liking but that’s besides the point. To recap, you can only use buttons locally, meaning only in Local Scripts, you cannot clone tools from the client and add it to said client, it just won’t work. Now using the above method will work, if you have any trouble just reply and we’ll get it sorted. If it does work, you know what to do, mark this as a solution thanks!