How can I change something that needs a localscript, with a serverscript?

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)
1 Like

Are there any errors in the output?

1 Like

I’m not sure if this relate with your question or not but you can try to use remote functions and remote events Bindable Events and Functions | Roblox Creator Documentation

1 Like

I’m going to give you a very broad answer, as I don’t have long to do this, Remote Events.

1 Like

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.

1 Like

I would use remote events but since im gonna add alot of swords, i have to make one script and 2 events for each sword

1 Like

Okay a small fix

Add a new RemoteEvent, fire that in the place of here. And then in a different server script just change this value.

Fire it like this:

YourRemoteEvent:FireServer(script.Parent.ItemNameValue.Value)

Server Script:

YourRemoteEvent.OnServerEvent:Connect(function(player, val)
    player:WaitForChild("equipValue").Value = val
end)
1 Like

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

1 Like

You can just use “for loops” to loop through all the swords and handle from 1 single script.

1 Like

Can you give me the link for the loop thing since I have no idea how to do that

Sure, here it is!

I’ve a question

What is the script.Parent?

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.

1 Like

Okay, it now makes sense. I’ll give you a quick little example in a bit.

Here’s a little bit of an example I prepared for you. Hope this helps!

Make sure, your sword frame is something like this:
image

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!

1 Like

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.

1 Like

You might be having trouble understanding the Roblox client-server model, which is imperative to making any sort of game. Luckily, the DevHub has great articles explaining it - Bindable Events and Functions | Roblox Creator Documentation, Client-Server Model | Roblox Creator Documentation and a visual of how it works. I suggest you always look at the hub before posting on the forum.

Here are some normal explanations I can give.

When you’re firing to the server from local script you have to do like this

RemoteEvent:FireServer(--[[Passing values is optional but don't send the player.]])

When receiving

RemoteEvent.OnServerEvent:Connect(function(player --[[Must be the player. It is set by default.]], --[[Any other values you sent]])

end)

When you’re firing to the client from server script you have to do like this

RemoteEvent:FireClient(player --[[Required]], --[[Other values are optional.]])

When receiving

RemoteEvent.OnClientEvent:Connect(function(--[[Make sure to only put the other values you have sent (If you have any).]])

end)
1 Like