So, I’ve been testing out a equip sword button where you hit the button and it gives you the Classic Sword. You hit it again and it’s removed from the inventory. Although this does work, the sword when equipped doesn’t work at all when it usually does in StarterPack. Does anyone know how to fix this?
local player = game.Players.LocalPlayer
local character = player.Character
local function GiveSwordToPlayer()
if character:FindFirstChild("ClassicSword") then
character.ClassicSword:Destroy()
else
local sword = game.ReplicatedStorage.ClassicSword
if sword then
local swordClone = sword:Clone()
swordClone.Parent = character
else
end
end
end
script.Parent.MouseButton1Click:Connect(GiveSwordToPlayer)
Because all of that is being handled from the client / a LocalScript, the sword is only cloned locally, which means that when the sword is given to the player that clicked the button, only they can see it (it’s not being replicated to the server and other players in the game).
To resolve that, communicate with the server via a RemoteEvent when the player presses the button so that the server can clone the sword and give it to the player, ensuring that it replicates / the sword becomes visible to everyone:
Example Revision
-- Script #1 (LocalScript code)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local RemoteEvent = ReplicatedStorage.RemoteEvent
local button = script.Parent
---
local debounce = false
local buttonPressCooldown = false
local optionalCooldownTime = 1
local function askServerToCreateSword()
if debounce == false then
debounce = true
RemoteEvent:FireServer()
if buttonPressCooldown == true then
task.wait(optionalCooldownTime) -- Optional time to wait if you don't want players to be able to spam the button
end
debounce = false
end
end
button.Activated:Connect(askServerToCreateSword)
-- Script #2 (Server script code)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClassicSword = ReplicatedStorage.ClassicSword
local RemoteEvent = ReplicatedStorage.RemoteEvent
---
RemoteEvent.OnServerEvent:Connect(function(player)
local Backpack = player:WaitForChild("Backpack")
local Character = player.Character or player.CharacterAdded:Wait()
if Backpack and Character then
local swordCheck = Backpack:FindFirstChild("ClassicSword") or Character:FindFirstChild("ClassicSword")
if swordCheck then
swordCheck:Destroy()
else
local clonedSword = ClassicSword:Clone()
clonedSword.Parent = Backpack
end
end
end)
-- Edit: Fixed typo; I forgot to include the closing parenthesis at the end
If you have any questions about how this works, feel free to ask!
I’ll try it to see if the code works. So, what you mean anyway is that I have to option to make it so other players can see the sword and not just the player by putting it into the serverscriptservice? Generally, I’m not sure where to put the 2nd script.
To make sure the new sword is visible to all players in the game (and subsequently works when someone tries to damage another player with the sword), the server would need to create it.
This means that the section of code responsible for creating the sword and giving it to the player is placed into a server-sided script. However, the code that handles the UI input (checking when a player presses the button), can remain in a LocalScript because it’s recommended to handle player input with the User Interface through client-sided scripts (since the button press itself doesn’t need to be replicated to other users, only the end action, which in this case, is the creation of a new sword).
The server-sided script can go in the ServerScriptService, as that’s one of the places where server scripts run and are often kept for organization purposes.
Is it fine that I have the tool Classic Sword in Replicated Storage and Local Script inside the UI button. Additionally, The code has a problem where this line clonedSword.Parent = Backpack is underlined in red. Says there is something to do with line 8. Sorry if I’m asking really basic questions, scripting is not my strong suit.
Woops, I just realized that I forgot the closing parentheses ) at the very last part of the function. Updating the final end to end) should resolve that. I’ll update the codeblock I posted to correct that, too.
Ok so I can’t shard the video because of an error, but imma just put it straight, the sword is no longer given to the player when the equip button is pressed.
Is the Classic Sword that’s being used in the game the official one from Roblox or is it a modified one? I tested the code included in my original response with the official sword uploaded by Roblox and it works whether it’s given to the player from the StarterPack or the server script.