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

Script does only work in play solo mode. Why? :(

Asked by 8 years ago
Edited by evaera 8 years ago

Ok, so here's the code:

repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character
local player1 = game.Players.LocalPlayer

wait(2)

local cash = player1:FindFirstChild('leaderstats').Cash
local selectedPart = player1:FindFirstChild('leaderstats').SelectedPart

function onClicked(playerWhoClicked)

    if (selectedPart.Value == 'Bad Conveyor') then

        cash.Value = cash.Value - 450
        local clone = game.ServerStorage.BadConveyor:Clone()
        clone.Parent = game.Workspace
        clone.Position = script.Parent.Position
    elseif (selectedPart.Value == 'Slow Conveyor') then

        cash.Value = cash.Value - 750
        local clone1 = game.ServerStorage.SlowConveyor:Clone()
        clone1.Parent = game.Workspace
        clone1.Position = script.Parent.Position

    elseif (selectedPart.Value == 'Normal Conveyor') then

        cash.Value = cash.Value - 1250
        local clone2 = game.ServerStorage.NormalConveyor:Clone()
        clone2.Parent = game.Workspace
        clone2.Position = script.Parent.Position

    elseif (selectedPart.Value == 'Fast Conveyor') then

        cash.Value = cash.Value - 1750
        local clone3 = game.ServerStorage.FastConveyor:Clone()
        clone3.Parent = game.Workspace
        clone3.Position = script.Parent.Position

    elseif (selectedPart.Value == 'Super Fast Conveyor') then

        cash.Value = cash.Value - 2100
        local clone4 = game.ServerStorage.SuperFastConveyor:Clone()
        clone4.Parent = game.Workspace
        clone4.Position = script.Parent.Position

    else
        print("ERROR")
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

I have a code that sets SelectedPart to 'Bad Conveyor', 'Slow Conveyor', 'Normal Conveyor', 'Fast Conveyor' or 'Super Fast Conveyor' so that isn't the problem.

It doesn't say any errors. It just won't work. Pls help. I'm a newbie to roblox lua.

1 answer

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

ServerStorage can't be used within Local Scripts, try changing the location of the items from ServerStorage to ReplicatedStorage.

http://wiki.roblox.com/index.php?title=API:Class/ServerStorage

Localplayer can't also be used in Server Scripts, you can easily get the player using the ClickDetector's function.

http://wiki.roblox.com/index.php?title=API:Class/ClickDetector

script.Parent.ClickDetector.MouseClick:connect(function(Player)
    local cash = Player:FindFirstChild('leaderstats').Cash
    local selectedPart = Player:FindFirstChild('leaderstats').SelectedPart
    if (selectedPart.Value == 'Bad Conveyor') then
        cash.Value = cash.Value - 450
        local clone = game.ReplicatedStorage.BadConveyor:Clone()
        clone.Parent = game.Workspace
        clone.Position = script.Parent.Position
    elseif (selectedPart.Value == 'Slow Conveyor') then
        cash.Value = cash.Value - 750
        local clone1 = game.ReplicatedStorage.SlowConveyor:Clone()
        clone1.Parent = game.Workspace
        clone1.Position = script.Parent.Position
    elseif (selectedPart.Value == 'Normal Conveyor') then
        cash.Value = cash.Value - 1250
        local clone2 = game.ReplicatedStorage.NormalConveyor:Clone()
        clone2.Parent = game.Workspace
        clone2.Position = script.Parent.Position
    elseif (selectedPart.Value == 'Fast Conveyor') then
        cash.Value = cash.Value - 1750
        local clone3 = game.ReplicatedStorage.FastConveyor:Clone()
        clone3.Parent = game.Workspace
        clone3.Position = script.Parent.Position
    elseif (selectedPart.Value == 'Super Fast Conveyor') then
        cash.Value = cash.Value - 2100
        local clone4 = game.ReplicatedStorage.SuperFastConveyor:Clone()
        clone4.Parent = game.Workspace
        clone4.Position = script.Parent.Position
    else
        print("ERROR")
    end
end)
0
It is not a local script and there not just thye cloning that doesn't work. It's everything. But I can try it anyways. ~Testing~ Disn't work. Tnx anyways kokungen12345 15 — 8y
0
You have "local player1 = game.Players.LocalPlayer" at the top of your script, you can only use that in a local script. Pawsability 65 — 8y
0
But if I put the code in a local script i don't work even in solo mode kokungen12345 15 — 8y
0
The thing is that it work perfectly well in solo mode but not at all when the game is published kokungen12345 15 — 8y
View all comments (3 more)
0
The thing is, you can't access LocalPlayers in server scripts. You made one of the most common mistakes. https://scriptinghelpers.org/blog/common-mistakes M39a9am3R 3210 — 8y
0
Ok, sorry, but how do I fix it? kokungen12345 15 — 8y
0
Try that. Pawsability 65 — 8y
Ad

Answer this question