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

In Roblox Studio when I test my game the LocalScript will not work, why?

Hi, I'm new to scripting ROBLOX LUA but I am already fluent in languages like C, C++, Java, and Python. I was wondering why this script won't work. I have a leader board setup and want to make this part change color when touched if you have enough points. I get no errors or even warnings but It won't do anything. Here is the localscript which is inside of the Part that I want to change color:

local OJ = game:GetService("ReplicatedStorage").OJ
OJ.Archivable = true
local player = game.Players.LocalPlayer
local buyOJ = script.Parent

local function touchedOJ(hit)
	local priceOJ = game.Workspace.Prices.ojPrice
	local playerPoints = player.leaderstats.Points.Value
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if(humanoid == nil) then
		if(playerPoints >= priceOJ) then
			buyOJ.BrickColor = BrickColor.new("Lime green")
		else
			buyOJ.BrickColor = BrickColor.new("Really blue")
			print("Not enough points!")
		end
	end
end

buyOJ.Touched:Connect(touchedOJ)

3 answers

WarmFireBlaz1 You are Very wrong for being 2 hours ago. Here you go!

local OJ = game:GetService("ReplicatedStorage").OJ
OJ.Archivable = true
local player = game.Players.LocalPlayer
local buyOJ = script.Parent

local function touchedOJ(hit)
    local priceOJ = game.Workspace.Prices.ojPrice.Value --You have not added .Value at the end
    local playerPoints = player.leaderstats.Points.Value
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if(humanoid == nil) then
        if(playerPoints >= priceOJ) then
            buyOJ.BrickColor = BrickColor.new("Lime green")
        else
            buyOJ.BrickColor = BrickColor.new("Really blue")
            print("Not enough points!")
        end
    end
end

buyOJ.Touched:Connect(touchedOJ)

So, you're making the two common mistakes of beginner scripting (we all have).

local OJ = game:GetService("ReplicatedStorage").OJ
OJ.Archivable = true
local player = game.Players.LocalPlayer
local buyOJ = script.Parent

local function touchedOJ(hit)
    local priceOJ = game.Workspace.Prices.ojPrice
    local playerPoints = player.leaderstats.Points.Value
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if(humanoid ~= nil) then -- I don't know how no one noticed this, but in your original script, you checked if the humanoid **WAS** nil.  Here you're checking if it **IS NOT** nil.
        if(playerPoints >= priceOJ) then
            buyOJ.BrickColor = BrickColor.new("Lime green")
        else
            buyOJ.BrickColor = BrickColor.new("Really blue")
            print("Not enough points!")
        end
    end
end

buyOJ.Touched:Connect(touchedOJ)

2 comments

0

I converted it to a script, and now I get this error. How would I fix it? Workspace.Part.Script:8: attempt to index upvalue 'player' (a nil value)

0

Ah, you'd need to remove player from line 3. Then, write, 'local player = game.Players:GetPlayerFromCharacter(hit.Parent)' on line 6 (right after the start of the function), then say 'if (humanoid ~= nil) and (player) then' instead of just 'if (humanoid ~= nil) then'.

Only Scripts work in Parts. Consider turning the localscript into a regular script. Many coders new to LUA often make this mistake... like I did.

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