Problem with variable

the remote event suppose to find a instance with the weapon tag and make it the weapon variable value, but when another fire the remote function it will assign the other player weapon to the variable, and all of the script is ran on their weapon. Trying to find a way around this its a script in Stater character script

local char = script.Parent

local weapon

RemoteEvent.OnServerEvent:Connect(function(plr, input, stamina)
	for i, v in pairs(plr.Character:GetChildren()) do
		if CollectionService:HasTag(v ,"weapon") then
			weapon = v
		end
	end
end)
2 Likes

If the weapon is a tool then you can use FindFirstChildOfClass(“Tool”).

1 Like

unfortunately its not a tool :frowning:

1 Like

put local weapon variable inside of the remote Event function,
cuz whenever the player Respawns the local weapon will be set to nil since the localscript is located in StarterCharacterScripts

This might help

remoteEvent.OnServerEvent:Connect(function(player)
	if player.Character == nil then return end
	local weapon = nil
	for i, child in player.Character:GetChildren() do
		if collectionService:HasTag(child, "weapon") == false then continue end
		weapon = child 
		break
	end
	print(weapon)
end)
1 Like

Server scripts don’t even run when placed inside the ‘StarterPlayerScripts’ container.

They do run?
Character limit Character limit

I meant ‘StarterPlayerScripts’ and it’s generally a bad idea to place server scripts inside the ‘StarterCharacterScripts’ container (as their existence replicates to the client).

1 Like

Try this

local char = script.Parent

RemoteEvent.OnServerEvent:Connect(function(plr, input, stamina)
    local weapon
	for i, v in pairs(plr.Character:GetChildren()) do
		if CollectionService:HasTag(v ,"weapon") then
			weapon = v
		end
	end
        -- do something with the variable
end)
--not here

It does make your game more vulnerable, delete a server script inside your character from the perspective of the client, the deletion will replicate to the server (because clients have network ownership over their own characters).

--Script inside 'StarterCharacterScripts'.
while task.wait(3) do
	print("Hello world!") --Stops printing once the script is deleted.
end

Oh I’m sorry, forgot Roblox still uses their awful client mechanism. (watch the mods delete this post)