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

Can anyone explain why this Script isn't working? attempt to index local 'Player' (a nil value)

This script is located within a TextButton in a ScreenGui. "colourval" is a BrickColor3 value which I've set to be created for each player that joins and have the name of the value to the player that has joined and I want this script to make the value change to whatever the TextButton BackgroundColor3 is. Any help?

local colour = script.Parent.BackgroundColor3




function onClick(Player) 
	local colourval = game:GetService("ServerStorage")[Player.Name]
	colourval.Value = colour
end

script.Parent.MouseButton1Click:connect(onClick)

output vvv

18:24:26.580 - Stack Begin 18:24:26.580 - Script 'Players.ShiforRBLX.PlayerGui.colourGui.mainFrame.TextButton.Script', Line 7 18:24:26.581 - Stack End

2 answers

The .MouseButton1Click function has no arguments, so “Player” will return nil. Instead, use “game.Players.LocalPlayer”.

Also, assuming you’re using a local script, you can’t access ServerStorage with a local script. You’ll need to use a server script for that. Instead, put your “colourval” in ReplicatedStorage, where both client and server can access.

Here is what your code should look like:

local colour = script.Parent.BackgroundColor3

function onClick() 
    local colourval = game:GetService("ReplicatedStorage")[game.Players.LocalPlayer.Name]
    colourval.Value = colour
end

script.Parent.MouseButton1Click:Connect(onClick)

Hoped this helped!

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