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

Why is my function (through ReplicatedStorage) coming up as a nil value?

Asked by 4 years ago
Edited 4 years ago

I've been re-reading and adjusting my code for quite awhile now and am fed up, I can't figure out why my function is coming up as a nil value.

The code in my ontouch script.

local req = require(game:GetService('ReplicatedStorage').updateHemp)
local interactable = true

function HempRegen()
    script.Parent.Transparency = 1
    wait(10)
    script.Parent.Transparency = 0
    interactable = true
end

function updateHemp()

end


local function onTouch(player)
    if interactable then
        local p = player.Parent.Name
        print(p)
        local collider = game.Players:WaitForChild(p)
        collider.leaderstats.Hemp.Value = collider.leaderstats.Hemp.Value + 1
        interactable = false
        UpdateHempCount(collider, 1)
        HempRegen()     
    end
end

script.Parent.Touched:Connect(onTouch)

The code in my modulescript which it is trying to call the function from.

local module = {}

local function UpdateHempCount(player, value)
    game.StarterGui.ScreenGui.TextLabel.text = "HEMP: " + player.leaderstats.Hemp.Value + value
end

return module

0
Include the function in the module table and then call the function by doing req.UpdateHempCount(). DeceptiveCaster 3761 — 4y
0
You never used req.UpdateHempCount (whatever Hemp is). To call a function from a module script use moduleScriptName.WhateverFunction. Phyrixia 51 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

There are a few errors I noticed here.

1) You are not using the required module to call the functions.

2) You cannot concatenate strings and values using +

3) You are trying to alter StarterGui which will have no affect on their screen.

4) You are not using the module to contain the function (I'll explain this one in detail)


Error 1

It's mainly not working because you have to use the required module to get the functions that are returned from it.

So, line 23 you are not using req, the required module.

Line 23 should look like this.

req.UpdateHempCount(collider, 1)

This will call the desired function from the module script with the required arguments.


Error 2

  • Is for adding values together. It's not for concatenating strings and values.

To concatenate strings and values you would use ..

So, In your module script on Line 4 you should change + to ..

Line 4 should look like this.

game.StarterGui.ScreenGui.TextLabel.text = "HEMP: "..player.leaderstats.Hemp.Value + value

Error 3

Anything being changed in StarterGui will have no affect on the players in the game. You would want to change the PlayerGui located inside of the player.

You would do player.PlayerGui.ScreenGui.TextLabel.Text

So, line 4 would look like this (ModuleScript)

player.PlayerGui.ScreenGui.TextLabel.Text = "HEMP: " + player.leaderstats.Hemp.Value + value

Error 4

You have to use the module to hold functions inside of the return method.

Without this, it'll run the function, but not for the player. Also, don't useLocal for functions inside of the ModuleScript.

So, your ModuleScript should look like this.

local module = {}

function module:UpdateHempCount(player, value)
    game.StarterGui.ScreenGui.TextLabel.text = "HEMP: " + player.leaderstats.Hemp.Value + value
end

return module

You can use . or : - It doesn't matter that much.


These are the final scripts.

ServerScript

local hempModule = require(game:GetService('ReplicatedStorage').updateHemp)
local interactable = true

local function HempRegen()
    script.Parent.Transparency = 1
    wait(10)
    script.Parent.Transparency = 0
    interactable = true
end

local function onTouch(player)
    if interactable then
        local p = player.Parent.Name
        print(p)
        local collider = game.Players:GetPlayerFromCharacter(player.Parent) --// Don't look for the player's name use :GetPlayerFromCharacter instead.
        collider.leaderstats.Hemp.Value = collider.leaderstats.Hemp.Value + 1
        interactable = false
        hempModule:UpdateHempCount(collider, 1)
        HempRegen()
    end
end

script.Parent.Touched:Connect(onTouch)

Module Script

local hempModule = {}

function hempModule:UpdateHempCount(player, value)
   player.PlayerGui.ScreenGui.TextLabel.Text = "HEMP: "..player.leaderstats.Hemp.Value + value
end

return hempModule

Hope this helped! If this worked, let me know by selecting this as an answer!

Ad

Answer this question