You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? For it to give the player the tool when clicked on. I want it to stay as a normal script and not a localscript, the script is inside of the button.
What is the issue? The player doesn’t receive the tool.
local Tool = game.ReplicatedStorage.Sword
script.Parent.MouseButton1Click:Connect(function(click,plr)
if plr.leaderstats.Credits.Value >= 0 then
local Clone = Tool:Clone()
Clone.Parent = plr.Character
end
end)
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
If this is the case you will need to use remote events and handle the tool cloning on the server rather than on the client. This is because changes made on the client are not replicated to the server and therefore wont affect the game or player. You can read up on remote events below
If it is a regular script you will need to change it to a local script and then have a regular script on the server to handle the cloning. The local script should fire a remote event which is then detected by your server script which then clones the tool and parents it to the players backpack
A regular Script is run on the server, while a LocalScript is run on the client. Unfortunately, the MouseButton1Click does not replicate to the server from the client. To fix this, you’ll want a LocalScript to connect to the click, and fire a RemoteEvent to the server, which will give the player the tool.
-- local script
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- get the ReplicatedStorage service
local GiveToolEvent = ReplicatedStorage:WaitForChild("GiveTool") -- get the GiveTool RemoteEvent
local Button = script.Parent -- get the button
Button.MouseButton1Click:Connect(function()
GiveToolEvent:FireServer() -- fire to the server
end)
-- server script
local ReplicatedStorage = game:GetService("ReplicatedStorage") -- get the ReplicatedStorage service
local Tool = ReplicatedStorage:WaitForChild("Sword") -- get the tool from ReplicatedStorage
local GiveToolEvent = Instance.new("RemoteEvent", ReplicatedStorage) -- create a RemoteEvent in ReplicatedStorage
GiveToolEvent.Name = "GiveTool" -- name the RemoteEvent "GiveTool"
GiveToolEvent.OnServerEvent:Connect(function(Player) -- connect to when the player fires the server
if Player.leaderstats.Credits.Value >= 0 then
local Character = Player.Character or Player.CharacterAdded:Wait()
local Clone = Tool:Clone()
Clone.Parent = Character
end
end)
To test this, just try replacing the event connection on the server with this
GiveToolEvent.OnServerEvent:Connect(function(Player) -- connect to when the player fires the server
print("Server event")
if Player.leaderstats.Credits.Value >= 0 then
print("Player has credits")
local Character = Player.Character or Player.CharacterAdded:Wait()
print("Giving tool")
local Clone = Tool:Clone()
Clone.Parent = Character
end
end)
Just replace the exact location of the previous Script, that you had made. This is where the LocalScript should be located. As long as it’s parented to the desired button, you should be fine.
The regular Script is best if put in ServerScriptService.