Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
2

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

Asked by
tane011 12
5 days ago

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

Log in to vote
0
Answered by 5 days ago
Edited 5 days ago

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)
Ad
Log in to vote
0
Answered by 5 days ago

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)

Answer this question