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

Why won't this place?

Asked by 8 years ago

My tool that places a shield doesn't work. The output shows no relative error whatsoever. It works in Studio, but not the in game. I have tried switching it from a script, to a localscript, but that only caused irrelevant errors (ServerStorage is not a valid member of DataModel(game)). Here is my code:

tool = script.Parent
db = true
r = 0
angle = 0
eq = false
player = tool.Parent.Parent 
    tool.Equipped:connect(function(mouse)
        eq = true

    mouse.Icon = "rbxasset://textures\\GunCursor.png"
    mouse.Button1Down:connect(function()
        if db and eq then
            db = false
            tool.Name = "[Shield; " .. angle .. "; Recharging]"
            mouse.Icon = "rbxasset://textures\\GunWaitCursor.png"
            local torso = tool.Parent:FindFirstChild("Torso")
            local shield = game.ServerStorage.Shield:Clone()
            shield.Parent = game.Workspace
            shield.Name = "Shield" .. r
            r = r + 1
            shield:SetPrimaryPartCFrame(CFrame.new(torso.CFrame.x, torso.CFrame.y - 3, torso.CFrame.z + 3) * CFrame.Angles(0, angle, 0) )
            game:GetService("Debris"):AddItem(game.Workspace:FindFirstChild(shield.Name), 60)

            wait(120)
            db = true
            tool.Name = "[Shield; ".. angle .. "; Ready]"
            mouse.Icon = "rbxaset://textures\\GunCursor.png"
        end
            mouse.KeyDown:connect(function(key)
        if key == "r" and db then

            if angle == 0 then
                angle = 90
            elseif angle == 90 then
                angle = 180
            elseif angle == 180 then
                angle = 0
            end

            tool.Name = "[Shield; " .. angle .. "; Ready]"

        end
    end)
    end)
end)
tool.Unequipped:connect(function()
        eq = false
    end)

Please help. (As far as I know,) There is absolutely no reason for it to not work.

1 answer

Log in to vote
0
Answered by 8 years ago

It works in studio because when you test something out there, you are both the server and the client.

In online mode, tools should use localscripts. Thus, the errors you got were not irrelevant, but an explanation of what you need to change.

ex, you got the error "ServerStorage is not a valid member of DataModel". This is because the clients don't receive ServerStorage. You need to move the Shield to ReplicatedStorage and have your script be a localscript and have it access the shield from ReplicatedStorage.

Notably, if you have FilteringEnabled, your script will need to be separated into a LocalScript and a Script with a RemoteEvent. The LocalScript would tell the Script when the player wants to activate a shield; the Shield would make sure that player is allowed to do that and - if so - activate the shield for that player. In this case, it's fine to keep the Shield in ServerStorage.

Ad

Answer this question