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

button makes arm colored while using rEvent. but, i can't utilize a localplayer inside a script?

This script allows the player to color their arm with a press of a button. But since it won't work without RemoteEvents, i tried to improvise.

However, when I did this, I ran into another error. How would I utilize a script that needs the localplayer when it's a normal script? So i tried to use playeradded. However it didn't work.

Does anyone have a solution?

Goal: Button "F" is supposed to change your arm to the color blue, and it's supposed to be visible to everybody (hence the use of RemoteEvents), but I can't do this on a normal script, because I can't utilize the localplayer.

Try this for yourself in Studio, use two players. And you'll see. On one player you can press "F" and it works. However, there's another bug, and it ends up making EVERYBODYS arm blue.

Can anyone help?

Script:

local tEvent = Instance.new("RemoteEvent")
tEvent.Name = "tEvent"
tEvent.Parent = script.Parent.Parent.ReplicatedStorage

game.Players.PlayerAdded:Connect(function(Player)
	
	local Character = Player.Character or Player.CharacterAdded:wait()

	local LUarm = Character:WaitForChild("LeftUpperArm")
	local CLUarm = LUarm:Clone()
	
	tEvent.OnServerEvent:Connect(function()
		
		print("Fired!")
		Player.Character.Humanoid.MaxHealth = 500
		
		CLUarm.CanCollide = false
        CLUarm.Anchored = false
        CLUarm.Color = Color3.new("Electric blue")
        CLUarm.Material = "Foil" 
        CLUarm.Size = Vector3.new(1.01, 1.2, 1.01)
        CLUarm.Parent = LUarm
		
		
	end)
end)


LocalScript:

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer
local tEvent = game.ReplicatedStorage:WaitForChild("tEvent")

repeat wait() until tEvent


local used = false

-- function
local function onInputBegan(input,gameProcessed)

    if input.KeyCode == Enum.KeyCode.F and used  == false then

        used = true
		
		tEvent:FireServer()


    elseif

        used == true then return end

end
UserInputService.InputBegan:connect(onInputBegan)

1 answer

✓ Accepted answer

Hey there, I think this can help ya; the first parameter of the OnServerEvent's function is always the player who fired the event. So your server script needs to be like this:


local tEvent = Instance.new("RemoteEvent")
tEvent.Name = "tEvent"
tEvent.Parent = script.Parent.Parent.ReplicatedStorage

tEvent.OnServerEvent:Connect(function(Player) -- Player who fired the event

	local Character = Player.Character or Player.CharacterAdded:wait()
	
	local LUarm = Character:WaitForChild("LeftUpperArm")
	local CLUarm = LUarm:Clone()
	
	
	
	print("Fired!")
	Player.Character.Humanoid.MaxHealth = 500
	
	CLUarm.CanCollide = false
	CLUarm.Anchored = false
	CLUarm.Color = Color3.new("Electric blue")
	CLUarm.Material = "Foil" 
	CLUarm.Size = Vector3.new(1.01, 1.2, 1.01)
	CLUarm.Parent = LUarm


end)


2 comments

0

thank you man!

0

No problem!

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