Can someone help me or tell me what It is I'm doing wrong?

I’ve been working on this goofy game, and I wanted to make some type of character customization.
The customization I added so far was being able to change the material of your player but when I tried to test the script it just changes all players material instead of the the main one who wants their material changed.

Button Script

local Player = game.Players.LocalPlayer

local PlrGui = Player.PlayerGui

local MaterialManager = PlrGui:WaitForChild("MaterialManager")

local VPMBody = MaterialManager.ViewportFrame.WorldModel.ViewportModel:WaitForChild("BODY")

local MatChangedRemote = game.ReplicatedStorage:WaitForChild("MaterialChanged")

script.Parent.MouseButton1Click:Connect(function()
	
	VPMBody.Material = Enum.Material.Neon
	
	MatChangedRemote:FireServer("Neon")
	
end)

Script that changes the player material

local MatChangedRemote = game.ReplicatedStorage:WaitForChild("MaterialChanged")

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		MatChangedRemote.OnServerEvent:Connect(function(plr, state)
			if state == "Neon" then
				local Body = character:WaitForChild("BODY")
				Body.Material = Enum.Material.Neon
			end
		end)
	end)
end)

Images:

I feel like this is an easy fix BUT I JUST CANT FIGURE IT OUT :sob:

If it helps the script that changes the player is in ServerScriptService

1 Like

The issue is this bit:

You should only define the OnServerEvent thing once, and use the plr variable to access the player’s character.

So the server script should be:

local MatChangedRemote = game.ReplicatedStorage:WaitForChild("MaterialChanged")

local Players = game:GetService("Players")

MatChangedRemote.OnServerEvent:Connect(function(plr, state)
	if state == "Neon" then
		local Body = plr.Character:WaitForChild("BODY")
		Body.Material = Enum.Material.Neon
	end
end)

Note that if the player dies, it won’t reupdate their character. If that is a requirement, let me know.

2 Likes

It works! if it’s ok can you pls add the part where it reupdates the material if the player dies?

Simplest way would probably be using a dictionary, something like this:

local MatChangedRemote = game.ReplicatedStorage:WaitForChild("MaterialChanged")

local Players = game:GetService("Players")

local Mapping = {}

Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        if Mapping[plr] then
            plr.Character:WaitForChild("BODY").Material = Mapping[plr]
        end
    end)
end)

MatChangedRemote.OnServerEvent:Connect(function(plr, state)
	if state == "Neon" then
		local Body = plr.Character:WaitForChild("BODY")
		Body.Material = Enum.Material.Neon
                Mapping[plr] = Enum.Material.Neon
	end
end)
1 Like

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