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

[SOLVED] I want to make a part move when a certain BoolValue is set to true but it isn't working?

Asked by 4 years ago
Edited 4 years ago

Hello, I've been working on this script for a few hours now but it isn't working. Basically, there's a BoolValue and when it's set to true, it's meant to make a part move when you press the keys WASD. Here's the code (The 1st is a LocalScript, the 2nd one is a Script).

local player = game.Players.LocalPlayer
local spaceship = game.Workspace:WaitForChild(player.Name.."Spaceship")
local WKeyHeld = false
local AKeyHeld = false
local SKeyHeld = false
local DKeyHeld = false
local EKeyHeld = false
local QKeyHeld = false
local g = workspace:WaitForChild("Main"):WaitForChild("gameStart")

-- NOTE: SpaceshipCamera is PrimaryPart

function onKeyPress(inputObject,gameProcessed)
    if g.Value == true then
        if inputObject.KeyCode == Enum.KeyCode.W then
            WKeyHeld = true
            while WKeyHeld do
                wait(0.0001)
                game.ReplicatedStorage:WaitForChild("Move"):FireServer(spaceship:WaitForChild(player.Name), spaceship:WaitForChild("SpaceshipCamera"), Vector3.new(0,0,-0.5))
            end
        end

        if inputObject.KeyCode == Enum.KeyCode.A then
            AKeyHeld = true
            while AKeyHeld do
                wait(0.0001)
                game.ReplicatedStorage:WaitForChild("Move"):FireServer(spaceship:WaitForChild(player.Name), spaceship:WaitForChild("SpaceshipCamera"), Vector3.new(-0.5,0,0))
            end
        end

        if inputObject.KeyCode == Enum.KeyCode.S then
            SKeyHeld = true
            while SKeyHeld do
                wait(0.00001)
                game.ReplicatedStorage:WaitForChild("Move"):FireServer(spaceship:WaitForChild(player.Name), spaceship:WaitForChild("SpaceshipCamera"), Vector3.new(0,0,0.5))
            end
        end

        if inputObject.KeyCode == Enum.KeyCode.D then
            DKeyHeld = true
            while DKeyHeld do
                wait(0.00001)
                game.ReplicatedStorage:WaitForChild("Move"):FireServer(spaceship:WaitForChild(player.Name), spaceship:WaitForChild("SpaceshipCamera"), Vector3.new(0.5,0,0))
            end
        end
    end
end

function onKeyRelease(inputObject,gameProcessed)
    if g.Value == true then
        if inputObject.KeyCode == Enum.KeyCode.W then
            WKeyHeld = false
        end
        if inputObject.KeyCode == Enum.KeyCode.A then
            AKeyHeld = false
        end
        if inputObject.KeyCode == Enum.KeyCode.S then
            SKeyHeld = false
        end
        if inputObject.KeyCode == Enum.KeyCode.D then
            DKeyHeld = false
        end
    end
end

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
game:GetService("UserInputService").InputEnded:Connect(onKeyRelease)
local spaceship = game.Workspace:WaitForChild("Spaceship")
local spaceshipMesh = spaceship:WaitForChild("SpaceshipMesh")
game.Players.PlayerAdded:Connect(function(v)
    local playerSpaceship = spaceship:Clone()
    playerSpaceship.Name = v.Name.."Spaceship"  playerSpaceship.SpaceshipMesh.Position = Vector3.new(-1.25, 24.75, 239)
    playerSpaceship.SpaceshipCamera.Position = Vector3.new(-1.8, 20, 265)
    playerSpaceship.Parent = game.Workspace
    playerSpaceship:WaitForChild("SpaceshipMesh").Anchored = false
    playerSpaceship:WaitForChild("SpaceshipMesh").Name = v.Name
    v:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("TextLabel").Visible = false
end)
game.ReplicatedStorage.Move.OnServerEvent:Connect(function(player, mesh, camera, direction)
    mesh.Position = mesh.Position + direction
    camera.Position = camera.Position + direction
end) 

Any help would be appreciated! :D

0
Local? or workspace? Nvm local scripts can move blocks but no one else can see them. Ill try to edit the script voidofdeathfire 148 — 4y
0
LocalScript ProvingTrottle 27 — 4y
0
The script is moving camera angles legit no block instances have been mentioned. Probably moving them on the main script voidofdeathfire 148 — 4y
0
It fires an ReplicatedEvent and then the server moves it from there. ProvingTrottle 27 — 4y
View all comments (3 more)
0
Your :FireServer(...) has to send (mesh, camera, direction). Right now you're sending (player, camera, direction). Arctanh 1 — 4y
0
I don't know why but the mesh moves too, the server seems to move the mesh aswell. ProvingTrottle 27 — 4y
0
Oh, I found out why! The game mesh is the name of the player, to make the racing system in my game more easy. ProvingTrottle 27 — 4y

1 answer

Log in to vote
0
Answered by 3 years ago

Apparently, Collision Disabled doesn't work, so I turned it back on and now it works!

Ad

Answer this question