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

Workspace.Part.Script:4: attempt to index nil with 'leaderstats'?

Asked by 2 years ago

it wont kick me when i touch the part

wait(5) local plr = game.Players.LocalPlayer if plr.leaderstats.Minutes.Value < plr.leaderstats.Minutes.Value == 100 then end script.Parent.Touched:Connect(function() if plr.leaderstats.Minutes < 100 then plr:Kick("Kicked | Hacking") end end)

0
please elaborate, also fix the formatting (use the buttons on top of the text box). Benbebop 1049 — 2y

1 answer

Log in to vote
1
Answered by 2 years ago

How's your day been?

I have discovered a problem with your code. I'm pretty sure you can't use game.Players.LocalPlayer in a script, and I'm also pretty sure you can't use the .Touched function in a LocalScript.

Let me fix this problem for you.

Go to the Home Tab and select Game Settings, then select Security. Then turn on 'Enable Studio to API Services'.

While your in the Security tab, you might as well turn on 'Allow HTTP requests' so the server can issue remote events.

Alright, now time for the second thing to do.

Insert a script inside ServerScriptService, and type in (or copy and paste) this code:

-- Let me explain what this does.
local store = game:GetService("DataStoreService") -- We get the DataStoreService
local data = store:GetDataStore("Minutes") -- Then we get the Data Store, I got the store 'Minutes'.

game.Players.PlayerAdded:Connect(function(plr) -- Wait till a player joins and get the 'plr' argument.
    local getData = data:GetAsync(plr.UserId) or 0 -- Find the player's data, if no data is found simply set the value to 0.
    local leaderstats = Instance.new("Folder",plr) -- Create a new folder inside our plr argument
    leaderstats.Name = "leaderstats" -- Name the folder "leaderstats"

    local mins = Instance.new("NumberValue",leaderstats) -- Create a number value inside leaderstats
    mins.Name = "Minutes" -- Name the value "Minutes"
    mins.Value = getData -- Make the value of 'mins' our data, in this case, let's say a new player who never played before joined. No data would be found so we would give them 0 minutes.

    while wait(60) do -- Start a loop which will fire every minute (a.k.a 60 seconds)
        mins.Value += 1 -- By the way, doing mins.Value = mins.Value + 1 is inefficient, instead, use mins.Value += 1, or mins.Value -=1. Anyhow...
    end
end)

game.Players.PlayerRemoving:Connect(function(plr) -- When a player leaves, get the plr argument.
    data:SetAsync(plr.UserId,plr.leaderstats.Minutes.Value)
end)

Now, onto your ACTUAL script!

Just kidding! Before all that you need to do 2 more things!

Add a RemoteEvent in ReplicatedStorage called "GetPlr".

Insert a LocalScript inside StarterPack and insert this script inside:

warn("Hello! This is a message from "..script.Name..". Giving the server the player...") -- Just a friendly hello from the client so we know its working!

game.ReplicatedStorage.GetPlr:FireServer() -- Incase your wondering why no arguments were put here, for the :FireServer() event (which only works on the client) doesn't need to have a player argument.

Now, FINALLY we're onto what you read this for! Your ACTUAL SCRIPT!

Just kidding! Before that you have to- Okay, but seriously, here's the script!

game.ReplicatedStorage.GetPlr.OnServerEvent:Connect(function(plr) -- Get our plr argument

    warn("Hello! The server has responded!") -- A friendly hello from the server so we know it's working

    if plr.leaderstats.Minutes.Value <= 100 then
        print("Player is not hacking. Good job, player, for having the decency on not to hack!") -- A reminder from the server that the player is decent enough not to hack
    end

    script.Parent.Touched:Connect(function() -- Fire a .Touched function
        if plr.leaderstats.Minutes.Value < 100 then
            plr:Kick("Kicked | Hacking") -- Kick the player if they ARE hacking
        end 
    end)

end)

This took a while, but it's ALL worth it to help people having problems!

With nothing else to say, goodbye, and tell me if you have any more errors or bugs.

Have a good day!

0
Also, it will kick them when they join (for some reason), also, next time when your making code, please use the code block. LikeableEmmec 470 — 2y
0
This is awesome :D Upvoted! 2Loos 168 — 2y
0
Thanks! It takes alot to add all the notes and make the script and explain, but it's worth it for a good, readable, working script. LikeableEmmec 470 — 2y
Ad

Answer this question