Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
1
Answered by
Lakodex 711 Moderation Voter
4 years ago

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)
Ad
Log in to vote
1
Answered by 4 years ago

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)
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) IProgram_CPlusPlus 58 — 4y
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'. MaximussDev 86 — 4y
Log in to vote
0
Answered by 4 years ago

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.

Answer this question