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

Why are my GUI and tool not working in the actual game but works in studio?

Asked by 6 years ago

Currently I am trying to make a non-experimental mode wizard battle game. The player has a certain amount of magic points they can use for spells. They can chose between three spells. When they equip their wand, they should see a GUI pop up telling them what spell they have selected. When they click, if they have enough magic points, the spell that they chose should go into action. The game works when I test by clicking on the play button but not on ROBLOX. I don't think it is the magic points because that works on ROBLOX. Thanks.

Here is my LocalScript in the tool:

local eq = false
local event = script.Parent:WaitForChild("RemoteEvent")
local spells = {"Light Bomb", "Lighting Rod", "Tidal Wave"}
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent
player.CharacterAdded:connect(function(character) 
    local spelltag = character:WaitForChild("SpellTag")
    local spellgui = player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("SpellLabel")
    local spell = spells[spelltag.Value]
    spellgui.Text = spell
    spelltag.Changed:connect(function()
        if eq then
            spell = spells[spelltag.Value]
            spellgui.Text = spell
        end
    end)

    local function onClick()
        if spell == "Light Bomb" then
            event:FireServer("Bomb", mouse.Hit)
        elseif spell == "Lighting Rod" then
            event:FireServer("Light", mouse.Hit, player)
        elseif spell == "Tidal Wave" then
            event:FireServer("Tidal", mouse.Hit, player)
        end
    end
    tool.Activated:connect(onClick)

    tool.Equipped:connect(function()
        eq = true
        spellgui.Visible = true
    end)

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

Here is the Server Script in the tool:

local event = script.Parent:WaitForChild("RemoteEvent")
local Connection
local magic = script.Parent.Parent.Parent.Character:WaitForChild("MagicTag")
function createBomb(location)
    local bomb = game:GetService("ServerStorage"):WaitForChild("lightbomb"):Clone()
    bomb.CFrame = location
    bomb.Parent = game.Workspace
end

function createTidal(location, player)
    local tidal = game:GetService("ServerStorage"):WaitForChild("tidalwave"):Clone()
    tidal.CFrame = script.Parent:WaitForChild("MagicPart").CFrame
    tidal:WaitForChild("NotKill").Value = player.Name
    tidal:WaitForChild("BodyPosition").Position = location.p
    tidal.Parent = game.Workspace
end

function raycast(location, player)
    local ray = Ray.new(script.Parent:WaitForChild("MagicPart").CFrame.p, (location.p - script.Parent:WaitForChild("MagicPart").CFrame.p).unit * 300)
    local part, position = game.Workspace:FindPartOnRay(ray, player.Character, false, true)
    local lightning = game:GetService("ServerStorage"):WaitForChild("lightning"):Clone()
    lightning.Parent = game.Workspace
    local distance = (script.Parent:WaitForChild("MagicPart").CFrame.p - position).magnitude
    lightning.Size = Vector3.new(0.3,0.3,distance)
    lightning.CFrame = CFrame.new(script.Parent:WaitForChild("MagicPart").CFrame.p, position) * CFrame.new(0,0, -distance / 2)
    game:GetService("Debris"):AddItem(lightning, 0.1)
end

local function decideEvent(player, ...)
    local tuple = {...}
    if tuple[1] == "Bomb" then
        if magic.Value >= 20 then
            magic.Value = magic.Value - 20
            createBomb(tuple[2])
        end
    elseif tuple[1] == "Tidal" then
        if magic.Value >= 35 then
            magic.Value = magic.Value - 35
            createTidal(tuple[2], tuple[3])
        end
    elseif tuple[1] == "Light" then
        if magic.Value >= 5 then
            magic.Value = magic.Value - 5
            raycast(tuple[2], tuple[3])
        end
    end
end

local tool = script.Parent
tool.Equipped:connect(function()
    Connection = event.OnServerEvent:connect(decideEvent)
end)

tool.Unequipped:connect(function()
    Connection:disconnect()
end)

1 answer

Log in to vote
0
Answered by 6 years ago

Without knowing the exact source of the problem, but instead given the information that it just doesn't work, makes finding a solution rather complicated. I am here to help you but reading 95 lines of code and trying to figure out how it all works, and then why it's not working, is not something I am willing to do at the moment.

Intead, I will give you advice on how to make finding solutions to these types of problems easier. The answer is simply, debugging. For a more lengthy tutorial on debugging you can go here, but I will give a shorter explanation.

Your best friend for debugging is print(), and your other best friend will be the Output bar. By litering your code in question with strategically placed print() commands you can see what parts of the code are running as intended and what parts are not. This way you can figure out why something is not working.

Of course the best way to start debugging (or placing print() commands) is to have a general idea of what's wrong in the first place. With this particular problem, we can safely assume the issue lies with Client to Server communication.

How do we know this? Since your game has Filtering Enabled on (non-experimental mode), Client to Server communication is somewhat limited. When playing your game normally the tool does not work (because FE is enabled), but when testing in studio it magically works! Why? Because when testing in studio normally the client and server are one in the same, so FE is rendered useless.

The best way to test your game when enabling FE is to go under the "Test" tab and open up a local server. If you do this, more than likely your tool will not be working. Infact, this can make debugging easy because sometimes you don't even need to use the print() command! Just open up the Output bar by going to the "View" tab and clicking the "Output" button. Then start a local server and look at the output. If you get any red text then your script is giving off an error, which is why it's not working. Fix the error and you should be good to go!

So what if there is no visible error? Now that we know what the general problem is, we can debugg! Simply go through your scripts and find all of the sections that deal with Client to Server communication and place print() commands with any recognizable text inside, that you will be looking for in the Output bar.

If your tool truely isn't working, then some of these messages you wrote won't be printed on the Output bar. You can then see at what point your script broke, and fix it! If they all aren't showing? Then maybe your search is a little to narrow, and the script broke before your first print command. Start by placing the prints earlier in the script, at the very start if you have to! And work your way down.

Hope this helps, good luck!

0
Thank you so much for that advice! I never would've thought why it didn't work! cool20134 2 — 6y
Ad

Answer this question