Yes, the equip script which also changes the StringValue’s name is a LocalScript and the script provided above is a normal script.
I think I’m starting to see how this has gone wrong now, lol.
Yes, the equip script which also changes the StringValue’s name is a LocalScript and the script provided above is a normal script.
I think I’m starting to see how this has gone wrong now, lol.
Anything that is made change from a LocalScript or client does not do effect on server. (except Position of network-owned parts like your body character). If you create a part from serverscript and attempt to changes it’s position from localscript, it’s position will not change on server perspective or other player’s perspective.
I see now.
Do you have any suggestions on what I should do in order for the server to recognize the StringValue change?
Everytime a player equip a sword from a shop, tell the server ‘hey! i want to equip this sword. Sword name : [sword name]’. Then the server clone the corresponsing sword from ReplicatedStorage.Swords and parenting them to the player’s backpack.
I would do this, however, I don’t really want the sword to be cloned when the player equips it.
Instead, the function above is used inside of a round system script so the equipped sword is only given once the round starts.
Adding on, I don’t wanna be spoon-fed script as some may assume lol, but do you think you could help me out on how to use a RemoteEvent in this instance? Where I would use it in the script and small guidance on how? As I typically work with UI and scripting isn’t really my strongest part. Thanks alot.
I think i misunderstand what you said lol. So what you mean is when there’s a round start, all players will be given the sword they equiped from the shop right?
Yeah, that’s correct.
(char limit)
As @Minhseu123 said changes made by LocalScripts on the client-side generally do not affect the server’s state or data, except for certain network-owned parts like a player’s character position. It’s important to use RemoteEvents or RemoteFunctions to communicate changes from the client to the server and vice versa when needed to maintain synchronization and ensure consistency across all players.
Im giving you a guide on how to implement this.
First, set up a RemoteEvent in ReplicatedStorage
named “EquippedSwordChangedEvent” as previously described.
In your LocalScript where the sword is equipped, you’ll use the RemoteEvent to notify the server about the change. Here’s how you can do it:
-- Assuming you have a reference to the RemoteEvent object
local equippedSwordChangedEvent = game.ReplicatedStorage:WaitForChild("EquippedSwordChangedEvent")
-- Example code where you change the StringValue's name
local equippedSwordName = "Steampunk Sword" -- Name of the sword
-- Fire off RemoteEvent to notify the server about the change
equippedSwordChangedEvent:FireServer(equippedSwordName)
On the server-side, you’ll listen for the RemoteEvent and handle the equipped sword change accordingly. Here’s how you can do it:
-- Assuming you have a reference to the RemoteEvent object
local equippedSwordChangedEvent = game.ReplicatedStorage:WaitForChild("EquippedSwordChangedEvent")
-- Define a function to handle the event
local function onEquippedSwordChanged(player, equippedSwordName)
-- Now you have the updated name of the sword and the player who equipped it
-- Perform any necessary logic here, such as giving the sword once the round starts
if player:GetAttribute("Team") then
local swordFolder = game.ReplicatedStorage:WaitForChild("Swords")
local equippedSword = swordFolder:FindFirstChild(equippedSwordName)
if equippedSword then
local newSword = equippedSword:Clone()
newSword.Parent = player.Backpack
else
warn("No sword found with name:", equippedSwordName)
end
else
warn("Player is not on a team:", player.Name)
end
end
-- Connect the function to the event
equippedSwordChangedEvent.OnServerEvent:Connect(onEquippedSwordChanged)
Now, whenever a player equips a sword from the shop, you’ll call the client-side script to fire the RemoteEvent. This event will notify the server, and the server will handle giving the player the equipped sword once the round starts.
This approach ensures that all players are given the sword they equipped from the shop when the round starts, using RemoteEvents to synchronize the data between the client and server. If you have any further questions or need clarification, feel free to ask!
Very good to know, thanks so much. I also recently found out that DataStore’s cant be used inside LocalScripts which is a bit irritating since I need to use it for something else.
Is it possible to store data using RemoteEvent’s? Out of curiousity.
Yes, you can use RemoteEvents to transmit data between client and server, but RemoteEvents themselves do not provide persistent storage like DataStores. However, you can use RemoteEvents in conjunction with DataStores to achieve a similar effect.
Just a little mistake. Whenever a player equip a sword, you don’t give the sword to them at that time, change the EquippedSword.Value to the sword they equip and when there’s a round start, all player will be given the sword they equip (Player
EquippedSword.Value). i’m bad at English lol.
My provided guide aligns with your suggestion. It sets up a RemoteEvent on the client-side to notify the server about the sword being equipped by the player. When the event is fired, it passes the name of the equipped sword to the server.
On the server-side, it listens for the RemoteEvent and handles the equipped sword change accordingly. It retrieves the player’s team attribute, locates the corresponding sword in the ReplicatedStorage, and clones it into the player’s Backpack.
It ensures that all players receive the sword they equipped from the shop once the round starts synchronization between the client and server.
Hey, just a question regarding this, where exactly would I put the bit where the EquippedSword name is changed? Do I keep it inside of the LocalScript of the EquipButton? This is how I have it set up right now, haven’t tested.
Local Script; what happens when equipButton is pressed:
equipButton.MouseButton1Click:Connect(function()
clickSound:Play()
if equipButton.Image == "rbxassetid://17316025753" then
if sword then
changeImage()
currentEquipped.Name = sword_to_give -- (sword_to_give is a variable which defines the swords name, for example "Steampunk Sword")
equippedSwordChangedEvent:FireServer(sword_to_give)
equipButton.Image = "rbxassetid://17316048949"
end
elseif equipButton.Image == "rbxassetid://17316048949" then
equipButton.Image = "rbxassetid://17316025753"
end
end)
Server Script; round system script and the function that gives a player the sword when called:
local function giveSword(player, equippedSwordName)
for _, player in pairs(Players:GetPlayers()) do
if not player:GetAttribute("Team") then continue end
local equippedSwordName = player:WaitForChild("EquippedSword").Name
if equippedSwordName then
local swordFolder = ReplicatedStorage:WaitForChild("Swords")
local equippedSword = swordFolder:FindFirstChild(equippedSwordName)
if equippedSword then
local newSword = equippedSword:Clone()
newSword.Parent = player.Backpack
else
warn("No sword found with name:", equippedSwordName)
end
else
warn("EquippedSword value not found for player:", player.Name)
end
end
end
equippedSwordChangedEvent.OnServerEvent:Connect(giveSword)
Your current setup looks mostly correct, but there are a couple of adjustments to make sure the funcionality:
currentEquipped.Name = sword_to_give
) and firing the RemoteEvent. Well done. In equippedSwordChangedEvent:FireServer(sword_to_give)
inside the MouseButton1Click
event for the equipButton. Just ensure that sword_to_give
is properly defined before using it here.Players:GetPlayers()
inside the giveSword
function. Since the function is already handling the event for a single player, you don’t need this loop. You can directly use the player
parameter received by the function.for _, player in pairs(Players:GetPlayers()) -- no necessary line
With this this how the code would look like
local function giveSword(player, equippedSwordName)
if not player:GetAttribute("Team") then
return
end
local swordFolder = ReplicatedStorage:WaitForChild("Swords")
local equippedSword = swordFolder:FindFirstChild(equippedSwordName)
if equippedSword then
local newSword = equippedSword:Clone()
newSword.Parent = player.Backpack
else
warn("No sword found with name:", equippedSwordName)
end
end
equippedSwordChangedEvent.OnServerEvent:Connect(giveSword)
With these adjustments, your implementation should work smoothly. Make sure to test it thoroughly to ensure everything functions as expected.
Will test everything out when I’m home and will let you know, thanks alot for your extended support.
Hey, I’m getting an error in output saying the following:
20:48:46.784 ServerScriptService.RoundSystem:187: attempt to index nil with 'GetAttribute' - Server - RoundSystem:187
This is using the updated script you provided.
if not player:GetAttribute("Team") then
Line the error is on. ^
Yea sorry about that, I forgot that when we’re calling the giveSword
function from the equippedSwordChangedEvent.OnServerEvent:Connect
, we’re passing the correct arguments.
Here’s how you should call the giveSword
function correctly
equippedSwordChangedEvent.OnServerEvent:Connect(function(player, equippedSwordName)
giveSword(player, equippedSwordName)
end)
This should be right
Inside of the server script for the round system, there are multiple functions called, would I still call the giveSword
function normally by using giveSword() or is there a different way to call it? The section where I call the giveSword() function is below, let me know if I keep it the same or adjust it to listen for the RemoteEvent being fired somehow.
while task.wait(1) do
if #Players:GetPlayers() > playersToStart then
for i = intermissionTime,1,-1 do
print(i)
Status.Value = "Intermission: "..i.. ""
task.wait(1)
end
-- Open Gates and show choices and let player choose
canPickTeam = true
openGate(true)
DisplayChoices()
ReplicatedStorage.MakePlayersInvisible:FireAllClients(true)
DropbothGates(false)
for i = PickSideTime,1,-1 do
print(i)
Status.Value = "Make a Choice! "..i.. ""
task.wait(1)
end
-- make the players re appear here and give sword
canPickTeam = false
openGate(false)
ReplicatedStorage.MakePlayersInvisible:FireAllClients(false)
DropbothGates(true)
giveSword() -- GIVESWORD FUNCTION CALLED
local beforeredTeam, beforeblueTeam = getPlayersTeams()
if #beforeredTeam == 0 or #beforeblueTeam == 0 then
Status.Value = "Unanimous!"
game.Workspace.TeleportPart1.CanCollide = true
game.Workspace.TeleportPart1.CanTouch = true
RestartRounds()
wait(1)
game.Workspace.TeleportPart1.CanCollide = false
game.Workspace.TeleportPart1.CanTouch = false
continue
end
for i = fightTime,1,-1 do
print(i)
Status.Value = "Fight! "..i.. ""
if i == fightTime then
playFightSound()
end
task.wait(1)
end
stopFightSound()
local redTeam, blueTeam = getPlayersTeams()
if #redTeam > #blueTeam then
Status.Value = "Blue Team wins!"
Rewardplayers("Red")
elseif #redTeam == #blueTeam then
Status.Value = "Both teams stand!"
else
Status.Value = "Red Team wins!"
Rewardplayers("Blue")
end
RestartRounds()
else
Status.Value = "Waiting for players.."
end
end
I tried to highlight the area where giveSword()
is called.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.