Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I need help! Can someone give me there version of a sword giving script?

I NEED HELP WITH THIS! I need to let the script know who is getting the sword and giving that person the sword when they press the button! Can someone help me, please?

My current script: (I need help with the "Player" part!)

local Button = script.Parent
local Sword = game.Lighting.ClassicSword
local Player = --Who is getting the sword

Button.MouseButton1Click:Connect(function()
	Sword:Clone()
	Sword.Name = "Starter Sword"
	Sword.CanBeDropped = false
	Sword.Parent = Player
end)

2 answers

✓ Accepted answer

This should solve your problems.

local Button = script.Parent
local Sword = game.Lighting.ClassicSword

Button.MouseButton1Click:Connect(function(Player) -- the player is defined here as the person who clicked the button
    Sword:Clone()
    Sword.Name = "Starter Sword"
    Sword.CanBeDropped = false
    Sword.Parent = Player.Backpack
end)

You will need to have two scripts to have this work, and a remote event. Put the RemoteEvent in ReplicatedStorage, the LocalScript aside the button, and the Server Script wherever.

First, the LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local giveRemote = ReplicatedStorage:WaitForChild("GiveRemote")
local button = script.Parent -- Make sure your LocalScript is parented to the part that you want clicked.
local clickDetector = Instance.new("ClickDetector", button)

local cooldown = false -- This is optional. It makes the script wait before you can press the button again.
local coolWait = 2

clickDetector.MouseButton1Click:Connect(function()
	if cooldown == false then
		giveRemote:FireServer()
		cooldown = true
		wait(coolWait)
		cooldown = false
	end
end)

Now, the Script (In the server)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local giveRemote = ReplicatedStorage:WaitForChild("GiveRemote")

giveRemote.OnServerEvent:Connect(function(player)
	Sword:Clone()
	Sword.Name = "Starter Sword"
	Sword.CanBeDropped = false
	Sword.Parent = Player
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.