Do something based on value contained in character

Every character in my game contains string value “mode” in HumanoidRootPart. Player can change this “mode” (please note that after changing “mode” player resets).
I have localscript in StarterCharacterScripts. It should run function based on mode.value (for example if mode.value = “Mayhem”, it runs Mayhem(character) function). When any player changes mode.value, function should run again.

Can someone help me do that? I feel like I don’t have enough experience and knowledge to do this correctly.

If someone interested:
Function Mayhem(character) renders aura for specific character. It was running in server script before, but because of slow server rendering aura was too laggy, so I moved this in LocalScript. Now to show other players’ aura I need to run this function from client-side for EVERY player.

1 Like

Basically this right?
Sorry for the continuous edits, I made it so you’re able to create your own functions through a table so it’s much cleaner. [edit]

local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Character = script.Parent

local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")
local mode = RootPart:WaitForChild("mode")

local Modes = {
    Mayhem = true,
    Corrupted = true
}

local ModeFunctions = {}

ModeFunctions.Mayhem = function(char)
    print(tostring(char) .. " selected Mayhem")
end

ModeFunctions.Corrupted = function(char)
    print(tostring(char) .. " selected Corrupted")
end

mode:GetPropertyChangedSignal("Value"):Connect(function()
    local NewMode = mode.Value

    if Modes[NewMode] then
        Humanoid.Health = 0
        local _function = ModeFunctions[NewMode]

        if _function then
            _function(Character)
        end
    end
end)
2 Likes

I just realized: this code works (I added some misc changes), but it doesn’t render other players’ modes

1 Like

So, that’s code I have currently:

local module2 = require(game.ReplicatedStorage.MODES.MAYHEM.Default)

local Players = game:GetService("Players")

local function modetrack(player)
	local character = player.character
	local mode = character:WaitForChild("HumanoidRootPart"):WaitForChild("mode")
	mode:GetPropertyChangedSignal("Value"):Connect(function()
		if mode.Value == "Mayhem" then
			module2:Mayhem(character)
		end
	end)
end

task.spawn(modetrack, Players:FindFirstChild("CEHATOP_PYCC"))

Function “modetrack(character)” works properly. But I need to run this function for every player since they joined and stop function for those who left

1 Like

Well, if you want other players to be able to see it you HAVE to communicate to the server somehow.

Local Script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Player = Players.LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")
local mode = RootPart:WaitForChild("mode")

local ChangeMode = ReplicatedStorage:WaitForChild("ChangeMode")

local Modes = {
	Mayhem = true,
	Corrupted = true
}

local ModeFunctions = {}

ModeFunctions.Mayhem = function(char)
	print(tostring(char) .. " is now Mayhem")
end

ModeFunctions.Corrupted = function(char)
	print(tostring(char) .. " is now Corrupted")
end

ChangeMode.OnClientEvent:Connect(function(plr, NewMode)
	if plr ~= Player then
		local char = plr.Character
		if char and ModeFunctions[NewMode] then
			ModeFunctions[NewMode](char)
		end
	end
end)

mode:GetPropertyChangedSignal("Value"):Connect(function()
	local NewMode = mode.Value
	if Modes[NewMode] then
		ChangeMode:FireServer(NewMode)
		Humanoid.Health = 0
		local _function = ModeFunctions[NewMode]
		if _function then
			_function(Character)
		end
	end
end)

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeMode = ReplicatedStorage:WaitForChild("ChangeMode")

local ValidModes = {
	Mayhem = true,
	Corrupted = true
}

ChangeMode.OnServerEvent:Connect(function(Player, NewMode)
	if ValidModes[NewMode] then
		ChangeMode:FireAllClients(Player, NewMode)
	end
end)
1 Like

If by player resets you mean that the character dies and they respawn with the mode they’ve chosen, then the way I would go about this is by having a script that is persistent (meaning that it doesn’t reset easily, for example in ServerScriptService), that detects player humanoid deaths. That script would act as a custom character loading controller(so you will have to disable automatic character loading), what it does is that upon death, it detects the current value of mode, then it moves the current value to the new character of the player (for persistency), among with the effect that it represents. After the new character is configured, it loads the player with the new character configuration, continuing the cycle (basically the persistent ServerScriptService script listens for characters/humanoids being added so it also applies to the new characters you load).

1 Like

I don’t think anything here should be handled by the client. You don’t want any player to be able to run that function whenever they want on all clients.