I’m trying to make an inventory gui that allows you to equip 1 sword at a time, but the problem is that when the equip button is clicked, the item value(StringValue) changes, and the equipped sword should have the same name as the item value, but since I changed the value thru a localscript, the server doesn’t recognise that change, and I don’t know how to fix this.
LocalScript inside the equip button:
local player = game.Players.LocalPlayer
local equipValue = player:FindFirstChild("EquippedSword")
script.Parent.MouseButton1Click:Connect(function(player)
script.Parent.Parent.Parent.EquipFrame.Image = script.Parent.Image
script.Parent.Parent.Parent.EquipFrame:FindFirstChild("ItemName").Text = script.Parent.ItemName.Text
script.Parent.Parent.Parent.EquipFrame:FindFirstChild("ItemName").BackgroundColor3 = script.Parent.ItemName.BackgroundColor3
equipValue.Value = script.Parent.ItemNameValue.Value
end)
LocalScript inside the Play button (since I want the player to get the weapon once they have started the game):
local player = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local Event = RS:FindFirstChild("Event04")
script.Parent.MouseButton1Click:Connect(function(player)
game.Lighting.Blur.Size = 0.1
script.Parent.Parent.Enabled = false
script.ClickSound:Play()
Event:FireServer()
end)
Script inside ServerScriptService:
local player = game.Players.LocalPlayer
local RS = game:GetService("ReplicatedStorage")
local Event = RS:FindFirstChild("Event04")
Event.OnServerEvent:Connect(function(player)
local equippedSword = player:FindFirstChild("EquippedSword") -- StringValue inside of the player
local sword = game.ServerStorage:FindFirstChild(equippedSword.Value) -- the sword tool inside of the ServerStorage
if player.Backpack:FindFirstChildOfClass("Tool") or player:FindFirstChildOfClass("Tool") then
player.Backpack:FindFirstChildOfClass("Tool"):Destroy()
wait(0.01)
local clone = item:Clone()
clone.Parent = player.Backpack
end
end)
If the EquippedSword value is nil, then there is an error at the item:Clone() part in the server Script, but it’s never really going to be nil, so there are none.
Yes, I would do that but as I said, I’m gonna add alot of swords which means that I have to make alot of server scripts and remote events and I’m pretty sure it’s gonna get laggy, so I was just wondering if there is another way to do this
the LocalScript inside the Play button’s script.Parent is the Play button and the LocalScript inside the equip button’s script.Parent is the equip button.
Here’s a little bit of an example I prepared for you. Hope this helps!
Make sure, your sword frame is something like this:
Local Script inside ScreenGui:
local player = game.Players.LocalPlayer
local frame = script:WaitForChild("Frame")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Swords = ReplicatedStorage:WaitForChild("SwordFolder")
local EquipButtonClickedRE = ReplicatedStorage:WaitForChild("EquipButtonClickedRE")
for i, v in pairs(Swords:GetChildren()) do
if v:IsA("Tool") then
local clone = frame:Clone()
clone.Parent = script.Parent -- Make sure the script is inside the ScreenGui and not anything else.
clone.EquipButton.MouseButton1Click:Connect(function()
-- Do stuff
-- ONLY MAKE SURE TO DO MODIFICATIONS WITH THE CLONE NOT THE MAIN FRAME
-- At last add this line
EquipButtonClickedRE:FireServer(clone:WaitForChild("ItemNameValue").Value)
end)
end
end
A Server script inside ServerScriptService:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EquipButtonClickedRE = ReplicatedStorage:WaitForChild("EquipButtonClickedRE")
EquipButtonClickedRE.OnServerEvent:Connect(function(player, valueToChange)
player:WaitForChild("EquippedSword").Value = valueToChange
end)
If you can’t understand what’s happening here, or any errors pop up make sure to reply back!
You don’t need to have multiple events. You can only have one remote event and one server script that handle the items like @AridFights1 suggested. Simply pass the item value as the first parameter of the event.