What do you want to achieve? Keep it simple and clear!
I want to create a script that gives the player a random item out of a Tools folder in Replicated storage.
What is the issue? Include screenshots / videos if possible!
I found A script on here that works, but when the player spawns in, other players don’t take damage.
What solutions have you tried so far? Did you look for solutions on the Developer Hub? I have tried moving the folder to ServerStorage, but it stops working at all.
This is a LocalScript
local RandomTool = game.ReplicatedStorage.Tools:GetChildren()[math.random(1, #game.ReplicatedStorage.Tools:GetChildren())] -- we get a random tool from the tools folder in the ReplicatedStorage
RandomTool.Parent = Player.Backpack -- Credits to DEVLocalPlayer
The weapon I’m using is one of the Roblox gun tools that are in the marketplace.
Whenever I clone the weapon from ReplicatedStorage into the local player’s backpack, other clients won’t take damage from the gun.
I see, the error is that the other player doesn’t take damage because it’s a local script.
So you should try with a script instead. For that we can use a Remote Event (Local script → Script in this case)
First of all create a RemoteEvent inside ReplicatedStorage.
I will call it GetPlayerEvent.
Create a local script inside Startergui.
local Player = game:GetService("Players").LocalPlayer
local event = game.ReplicatedStorage.GetPlayerEvent
event:FireServer(Player)
The local script gets the local player, and send it in the RemoteEvent.
Now create a script inside ServerScriptService, I will use a bit of your script that you provided.
local event = game.ReplicatedStorage.GetPlayerEvent
local RandomTool = game.ReplicatedStorage.Tools:GetChildren()[math.random(1, #game.ReplicatedStorage.Tools:GetChildren())]
event.OnServerEvent:Connect(function(player)
RandomTool.Parent = player.Backpack
end)
The script gets from the RemoteEvent the Client, the LocalPlayer, and it gives to him the random item.
So THAT’S what the brackets are for.
Thanks a TON, I never would have thought of that.
Also, seems like I need to clone the item before sending it to the player, although all I did was put a :clone() after the variable is called to the backpack.
Nonetheless, Thanks for helping!
It seems an issue still persists.
Whenever the player joins the game, they get 2 weapons, however, these weapons do not change when the player dies, and they get the same weapons until they leave the game. this is bad, since it is supposed to change every time the player respawns.
The game link is here if you want to see for yourself: Game Here
Sorry about the sudden reply, I just saw this issue.
The script is defining the random tool and then giving it to the player when they send the event, but it isn’t switching the tools when they send it again. That’s because the RandomTool value is defined outside of the OnServerEvent event, meaning it will always remain the same.
Fix it by putting the RandomTool value inside of the event function.
I think I see the problem. Because the player gets the tool from a local script, it only happens on the client side and not the server side. If I were you, I would change the script to be a normal script
But don’t put the script in player. Just put it into the tool in replicated storage
However, the problem is that the script doesn’t seem to recognize the RandomTool variable.
If someone could explain this to me, that would be nice.
Thanks.