Random Item script doesn't deal damage

  1. 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.
  2. 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.
  3. 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

I can’t provide any videos at the moment, sorry.

Anything helps!

What do you mean they don’t take damage? Do you mean when the tool is activated? Do you have anything to handle the tool being activated?

This line of code might help:
targetPlayer.Character.Humanoid:TakeDamage(SomeDamageHere)

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.

Here’s what the entire tool system looks like.


I’ll provide a recording soon.

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)

  1. 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.

  1. 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.

I hope I helped you :wink:

1 Like

If that helped, you can mark that as solved.

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!

:hugs:

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. :man_facepalming:

I see the problem.

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

So, You’re saying that I should define the tool in the local script,

RandomTool = game.ReplicatedStorage.Tools:GetChildren()[math.random(1, #game.ReplicatedStorage.Tools:GetChildren())]

Then send it through the event call,

event:FireServer(Player, RandomTool)

so that the server script can pick it up and send it.

event.OnServerEvent:Connect(function(player)
	RandomTool:Clone().Parent = player.Backpack
end)

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.

So, instead you want that when a player reset they will get another tool and clone it?

Yes, That’s exactly what I need. I thought you could simply carry over variables through event triggers, but that doesn’t seem to be the case.

That will not work correctly, this will be just done at the spawn.