Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why isn't anyone losing their weapons?

I am trying to make a couple of scripts where if someone with a certain role dies, every player in the server loses any weapons they might have. However, this only works in studio for some reason and doesn't work in the actual game. What is the problem?

Local Script:

player = game.Players.LocalPlayer
character = player.Character or player.CharacterAdded:Wait()
humanoid = character:WaitForChild("Humanoid")
nightguardGone = game.ReplicatedStorage.Remotes.NightguardGone
removeWeapons = game.ReplicatedStorage.Remotes.RemoveWeapons

humanoid.Died:Connect(function()
	if player:WaitForChild("Info").Role.Value == "Nightguard" then
		nightguardGone:FireServer()
		removeWeapons:FireServer()
	end
end)

Server Script:

removeWeapons = game.ReplicatedStorage.Remotes.RemoveWeapons

removeWeapons.OnServerEvent:Connect(function(player)
	for _, plr in pairs(game.Players:GetPlayers()) do
		for _, tool in pairs(plr.Backpack:GetChildren()) do
			tool:Destroy()
		end
	end
	
	print("Took Possible Weapon From Player")
end)

And to clarify, yes it does indeed print.

1 answer

Here's both my rewrite of the server script and the local script! I hope that my version does help the script run a bit better. If there's still an issue, you could reply to this answer with the output's error if there is one.

-- Server Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoveWeapons = ReplicatedStorage.Remotes:FindFirstChild("RemoveWeapons")
repeat
	wait()
until RemoveWeapons ~= nil
RemoveWeapons.OnServerEvent:Connect(function(Player)
	pcall(function()
		for i,v in pairs(Player.Character:GetChildren()) do
			if v:IsA("BackpackItem") then
				v:Destroy()
			end
		end
	end)
	pcall(function()
		for i,v in pairs(Player:FindFirstChildOfClass("Backpack"):GetChildren()) do
			if v:IsA("BackpackItem") then
				v:Destroy()
			end
		end
	end)
end)

-- LocalScript

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

repeat
	wait()
until Humanoid ~= nil

local NightGaurd = ReplicatedStorage.Remotes.NightgaurdGone
local RemoveWeapons = ReplicatedStorage.Remotes.RemoveWeapons

Humanoid.Died:Connect(function()
	local Info = LocalPlayer:FindFirstChild("Info");
	repeat
		wait()
	until Info ~= nil and Info:FindFirstChild("Role");
	if string.lower(Info.Role.Value) == ("nightgaurd") then
		NightGaurd:FireServer()
		RemoveWeapons:FireServer()
	end
end)

1 comment

0

i was thinking thar

This discussion is preserved as it was. New answers, comments, and votes are closed.