Server -> Client -> Server because idk how to use RemoteFunction

i hate remote event so bad
Here are the scripts i use to make the local player change team when they touch an NPC

local NPCHRP = script.Parent:FindFirstChild("Humanoid")
local players = game:GetService("Players")
local events = game:GetService("ReplicatedStorage")
function onTouched(hit)
	if script.Parent.Humanoid.Health > 0 then
		if script.Parent.Configs.ChasesNPCs.Value == false and game.Players:FindFirstChild(hit.Parent.Name) or script.Parent.Configs.ChasesNPCs.Value == true then
			local human = hit.Parent:findFirstChild("Humanoid")
			if (human ~= nil) then
                        game.ReplicatedStorage.Death:FireClient(player)
end
end
end
NPCHRP.Touched:connect(onTouched)

^above is a server script in workspace

local players = game:GetService("Players")
local team = game:GetService("Teams")
local player = game.Players.LocalPlayer

game.ReplicatedStorage.Death.OnClientEvent:Connect(function()
	game.ReplicatedStorage.Death2:FireServer()
end)

^above is a local script i put in StarterPlayerScript

local player = game:GetService("Players").PlayerAdded:Wait()


game.ReplicatedStorage.infected2.OnClientEvent:Connect(function()
	player.TeamColor = BrickColor.new("White")
end)

^ this one is the children of that Death2 remote event
the output return no error
these scripts are actually pretty essential to my game too so i really need a solution

quick edit: im new to scripting too

You can set the player’s TeamColor directly from the server without needing to fire the RemoteEvent to the client and then back to the server:

Example Revision

local Players = game:GetService("Players")

local NPC = script.Parent
local npcHumanoid = NPC:WaitForChild("Humanoid")
local Configs = NPC:WaitForChild("Configs")
local ChaseNPCs = Configs:WaitForChild("ChaseNPCs")

npcHumanoid.Touched:Connect(function(hit)
    if npcHumanoid.Health <= 0 then return end

    local characterCheck = hit:FindFirstAncestorOfClass("Model")
    local player = Players:GetPlayerFromCharacter(characterCheck)

    if not characterCheck then return end
    if not player then return end

    if ChaseNPCs.Value ~= nil then
        player.TeamColor = BrickColor.new("White")
    end
end)
2 Likes

why are you using remotes? use bindable events?
also you have some many variables but you don’t even use them!

you can do it fully on the server by using bindable events:

local players = game.Players
local parent = script.Parent
local Humanoid = parent:FindFirstChild("Humanoid")
local repli = game.ReplicatedStorage
local chasesNPC = parent.Configs.ChasesNPC

Humanoid.Touched:Connect(function(hit, limb)
  if Humanoid.Health <= 0 then return end
  local player = players:GetPlayerFromCharacter(hit.Parent)
  --if script.Parent.Configs.ChasesNPCs.Value == false and game.Players:FindFirstChild(hit.Parent.Name) or script.Parent.Configs.ChasesNPCs.Value == true then
--wth is this check, it checks if the value is false and hit is a player, or the value is true?
  if not chasesNPC.Value and player or chasesNPC.Value then
    local human = hit.Parent:FindFirstChild("Humanoid")--why check if plr exists, if you're using it anyway.
    if not human then return end
    repli.BindableEvent:Fire(player)
  end
end)

repli.BindableEvent.Event:Connect(function(player)
  player.TeamColor = Color3.new(1, 1, 1)--brickcolor will error.
end)

also even better, just use a function:

local players = game.Players
local parent = script.Parent
local Humanoid = parent:FindFirstChild("Humanoid")
local chasesNPC = parent.Configs.ChasesNPC

local function setTeam(plr)
  plr.TeamColor = Color3.new(1, 1, 1)
end
Humanoid.Touched:Connect(function(hit, limb)
  if Humanoid.Health <= 0 then return end
  local player = players:GetPlayerFromCharacter(hit.Parent)
  --if script.Parent.Configs.ChasesNPCs.Value == false and game.Players:FindFirstChild(hit.Parent.Name) or script.Parent.Configs.ChasesNPCs.Value == true then
--wth is this check, it checks if the value is false and hit is a player, or the value is true?
  if not chasesNPC.Value and player or chasesNPC.Value then
    local human = hit.Parent:FindFirstChild("Humanoid")--why check if plr exists, if you're using it anyway.
    if not human then return end
    setTeam(player)
  end
end)

and even better, just do it directly!

local players = game.Players
local parent = script.Parent
local Humanoid = parent:FindFirstChild("Humanoid")
local chasesNPC = parent.Configs.ChasesNPC

Humanoid.Touched:Connect(function(hit, limb)
  if Humanoid.Health <= 0 then return end
  local player = players:GetPlayerFromCharacter(hit.Parent)
  --if script.Parent.Configs.ChasesNPCs.Value == false and game.Players:FindFirstChild(hit.Parent.Name) or script.Parent.Configs.ChasesNPCs.Value == true then
--wth is this check, it checks if the value is false and hit is a player, or the value is true?
  if not chasesNPC.Value and player or chasesNPC.Value then
    local human = hit.Parent:FindFirstChild("Humanoid")--why check if plr exists, if you're using it anyway.
    if not human then return end
    player.TeamColor = Color3.new(1, 1, 1)--directly in if stmt
  end
end)

I don’t know why some design choices were made, but hope this helps!
what even is Chasesnpc for btw?

1 Like

I don’t even know it existed :sob:

1 Like

How didn’t I know about that
Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.